0

I have a NodeJS + Express application.

When running on localhost, I use

const port = 3001;

server.listen(port, () => {
  console.log(`App listening on port ${port}`);
});

When deploying to GAE, I use

const port = process.env.PORT || 8080;

app.listen(port, () => {
  console.log(`App listening on port ${port}`);
});

It was working fine until today, when I needed to implement Sockets.io on my application.

Sockets requires a server listening server.listen(), but GAE doesn't seem to accept this method, and only accepts app.listen() instead.

What am I doing wrong?

Pedro Rabbi
  • 193
  • 1
  • 12

1 Answers1

0

So, these two methods of coding are fundamentally equivalent:

const server = http.createServer(app);
server.listen(port);

And:

const server = app.listen(port);

All, app.listen() does internally is call http.createServer(app) and then call server.listen(). So, app.listen() is just a shortcut. Here's more on app.listen() here.

If you need the server instance for use with socket.io, then just assign it like I show in const server = app.listen(port); and then use that server instance to pass to socket.io for it to share your http server.


Note, if you want to create an https server, then you have to do it the first way I show above (and pass the security credentials) because app.listen() does not create an https server.

jfriend00
  • 683,504
  • 96
  • 985
  • 979