Authenticating socket io connections using JWT

It doesn’t matter if the token was created on another server. You can still verify it if you have the right secret key and algorithm. Implementation with jsonwebtoken module client const {token} = sessionStorage; const socket = io.connect(‘http://localhost:3000’, { query: {token} }); Server const io = require(‘socket.io’)(); const jwt = require(‘jsonwebtoken’); io.use(function(socket, next){ if (socket.handshake.query … Read more

Declare multiple module.exports in Node.js

You can do something like: module.exports = { method: function() {}, otherMethod: function() {}, }; Or just: exports.method = function() {}; exports.otherMethod = function() {}; Then in the calling script: const myModule = require(‘./myModule.js’); const method = myModule.method; const otherMethod = myModule.otherMethod; // OR: const {method, otherMethod} = require(‘./myModule.js’);

Can mongo upsert array data?

I’m not aware of an option that would upsert into an embedded array as at MongoDB 2.2, so you will likely have to handle this in your application code. Given that you want to treat the embedded array as sort of a virtual collection, you may want to consider modelling the array as a separate … Read more

Expressjs raw body

Something like this should work: var express = require(‘./node_modules/express’); var app = express.createServer(); app.use (function(req, res, next) { var data=””; req.setEncoding(‘utf8’); req.on(‘data’, function(chunk) { data += chunk; }); req.on(‘end’, function() { req.body = data; next(); }); }); app.post(“https://stackoverflow.com/”, function(req, res) { console.log(req.body); }); app.listen(80);