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
2 Answers
Reset to default 5You 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");
});