5

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 completely different NodeJS server which will host only the GRPC server.

Asutosh
  • 1,775
  • 2
  • 15
  • 22
  • There is a blogpost+repo by Diego Garcia, that says that it is possible. However it uses the Go language https://medium.com/@drgarcia1986/listen-grpc-and-http-requests-on-the-same-port-263c40cb45ff – neoneye Mar 29 '20 at 22:48

2 Answers2

4

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.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
0

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");
  });
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265