vgokul129 vgokul129 - 11 days ago 8
Node.js Question

next() call in express-ws module in nodejs

I came across a sample code for my webserver using express-ws for reliable Websocket connection between my server and client browser. The code will start listening in port 3000 for websocket connection.

var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'testing';
return next();
});

app.get('/', function(req, res, next){
console.log('get route', req.testing);
res.end();
});

app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
});
console.log('socket', req.testing);
});

server.listen(3000);


what is the use of next() API in express-websocket?

Answer

It calls the next middleware in the stack. Explained Here

Comments