最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can grpc and express server run by same nodejs server, or grpc has to be different server - Stack Overflow

programmeradmin4浏览0评论

I am trying to create a REST server which will be based on Node/Express. How to add a GRPC server in the same REST server, or it has to be pletely different NodeJS server which will host only the GRPC server.

I am trying to create a REST server which will be based on Node/Express. How to add a GRPC server in the same REST server, or it has to be pletely different NodeJS server which will host only the GRPC server.

Share Improve this question edited Jul 4, 2019 at 10:06 Asutosh asked Jul 4, 2019 at 5:11 AsutoshAsutosh 1,8282 gold badges16 silver badges22 bronze badges 1
  • There is a blogpost+repo by Diego Garcia, that says that it is possible. However it uses the Go language medium./@drgarcia1986/… – neoneye Commented Mar 29, 2020 at 22:48
Add a ment  | 

2 Answers 2

Reset to default 5

You cannot add a gRPC server to an Express server. You can run a gRPC server in the same process as an Express server, but they will serve on separate ports and run independently.

This is what I did which is basically triggering the GRPC server start on the listen callback of express

import express from "express";

import { Server,  ServerCredentials } from "grpc";

const server = new Server();
server.bind('0.0.0.0:50051', ServerCredentials.createInsecure());

const router = express.Router();

express()
  .use("/", router)
  .listen(3000, () => {
    server.start();
    console.log("listening");
  });
发布评论

评论列表(0)

  1. 暂无评论