socket.io and session?

This won’t work for sockets going over the flashsocket transport (it doesn’t send the server the needed cookies) but it reliably works for everything else. I just disable the flashsocket transport in my code. To make it work, in the express/connect side, I explicitly define the session store so I can use it inside socket: … Read more

How to share sessions with Socket.IO 1.x and Express 4.x?

The solution is surprisingly simple. It’s just not very well documented. It is possible to use the express session middleware as a Socket.IO middleware too with a small adapter like this: sio.use(function(socket, next) { sessionMiddleware(socket.request, socket.request.res, next); }); Here’s a full example with express 4.x, Socket.IO 1.x and Redis: var express = require(“express”); var Server … Read more

Why can I execute code after “res.send”?

Sure end ends the HTTP response, but it doesn’t do anything special to your code. You can continue doing other things even after you’ve ended a response. What you can’t do, however, is do anything useful with res. Since the response is over, you can’t write more data to it. res.send(…); res.write(‘more stuff’); // throws … Read more

How to structure a express.js application?

I have mine broken up as follows: ~/app |~controllers | |-monkey.js | |-zoo.js |~models | |-monkey.js | |-zoo.js |~views | |~zoos | |-new.jade | |-_form.jade |~test | |~controllers | |-zoo.js | |~models | |-zoo.js |-index.js I use Exports to return what’s relevant. For instance, in the models I do: module.exports = mongoose.model(‘PhoneNumber’, PhoneNumberSchema); and then … Read more

Uploading images using Node.js, Express, and Mongoose

I’ll answer my own question for the first time. I found an example straight from the source. Please forgive the poor indentation. I wasn’t sure how to indent properly when copying and pasting. The code comes straight from Express multipart/form-data example on GitHub. // Expose modules in ./support for demo purposes require.paths.unshift(__dirname + ‘/../../support’); /** … Read more

Why should I use Restify?

Corrigendum: this information is now wrong, keep scrolling! there was an issue with the script causing the Restify test to be conducted on an unintended route. This caused the connection to be kept alive causing improved performance due to reduced overhead. This is 2015 and I think the situation has changed a lot since. Raygun.io … Read more