Asutosh
Asutosh

Reputation: 1828

Can grpc and express server run by same nodejs server, or grpc has to be different 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 completely different NodeJS server which will host only the GRPC server.

Upvotes: 5

Views: 3988

Answers (2)

Archimedes Trajano
Archimedes Trajano

Reputation: 41480

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");
  });

Upvotes: 0

murgatroid99
murgatroid99

Reputation: 20297

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.

Upvotes: 5

Related Questions