0

I have the following application that started out with Express as shown below:

const express = require('express');
const app = express();
let port = process.env.PORT||8080;

app.listen(port, async function () {
  console.log(`Service running on port ${port} in ${app.get("env")} mode`);
});

However, I would like to use gRPC as well in same service. Unfortunately I can't because the gRPC server also need it's own separate PORT which has already been used by the Express Server.

Is there another way to get around this without spinning two containers for both servers?

I want both servers to use same PORT as my target environment is Google Cloud Run.

And unfortunately Google Cloud Run supports spinning only 1 container per service instance, so I'm seriously out of luck sort of from my own experience.

ololo
  • 1,326
  • 2
  • 14
  • 47
  • See: https://stackoverflow.com/questions/56881021/can-grpc-and-express-server-run-by-same-nodejs-server-or-grpc-has-to-be-differe – jfriend00 Nov 27 '22 at 21:57
  • I recommend splitting the services into separate container instances. Otherwise, you will need to configure a proxy to split the traffic. – John Hanley Nov 27 '22 at 22:04
  • @jfriend00 I saw that, but that's only applicable in the Go community. I wish the NodeJS community had something similar as I'm more of a NodeJS guy – ololo Nov 27 '22 at 22:09
  • I'm confused. The answers in the article I linked are about nodejs programming. I'm not referring to the Go article. FYI, it's probably possible to construct a proxy that will separate out the traffic from the two protocols on the same port - it appears someone may have done something like that in a different language, but I didn't see anything like that for nodejs. – jfriend00 Nov 27 '22 at 22:38

1 Answers1

0

Each language has its own implementation for gRPC. There are many differences from each language implementation, some due to language capability but also due to the maintainers.

As already shared a Stackoverflow link in the comment section by jfriend00, which stated that

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”

Currently, the Node standard library does not provide an equivalent feature as Go, so you can't share the port which is applicable in the Go community.

For more information, you can refer to these Stackoverflow Link which may help you.