express js app listen vs server listen

Solutions on MaxInterview for express js app listen vs server listen by the best coders in the world

showing results for - "express js app listen vs server listen"
Matteo
16 Jul 2018
1//The second form (creating an HTTP server yourself, instead of having Express create one for you) is useful if you want to reuse the HTTP server, for example to run socket.io within the same HTTP server instance:
2
3var express = require('express');
4var app     = express();
5var server  = require('http').createServer(app);
6var io      = require('socket.io').listen(server);
7...
8server.listen(1234);
9//However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
10
11var express   = require('express');
12var app       = express();
13
14// app.use/routes/etc...
15
16var server    = app.listen(3033);
17var io        = require('socket.io').listen(server);
18
19io.sockets.on('connection', function (socket) {
20  ...
21});