How to access session in express, outside of the req?

I know that I can use

function(req, res) {

using express. However I need to access the session outside of the response function. How would I go about doing that?

I’m using to pass information for adding posts and comments. So when I receive the message on the server-side, I need to verify the person posting the information by using the session. However since this is being done via there is no req/res.

I think I have a different answer.


var MongoStore = require('connect-mongo')(session);
var mongoStore = new MongoStore({
    db:settings.db,            //these options values may different
    store : mongoStore
    //here may be more options,but store must be mongoStore above defined

then you should define a session key at req,just like :



finally,you can get it this way:


var cookie = require("cookie"); //it may be defined at the top of the file

 var tS = cookie.parse(connection.handshake.headers.cookie)['connect.sid'];
 var sessionID = tS.split(".")[0].split(":")[1];

I had test it yesterday, it worked well.

Using, I’ve done this in a simple way. I assume you have an object for your application let’s say MrBojangle, for mine it’s called Shished:

 * Shished singleton. 
 * @api public
function Shished() {

Shished.prototype.getHandshakeValue = function( socket, key, handshake ) {                          
    if( !handshake ) {
        handshake = socket.manager.handshaken[ ];                                         
    return handshake.shished[ key ];                                                                

Shished.prototype.setHandshakeValue = function( socket, key, value, handshake ) {                   
    if( !handshake ) {
        handshake = socket.manager.handshaken[ ];                                         
    if( !handshake.shished ) {
        handshake.shished = {};                                                                     
    handshake.shished[ key ] = value;                                                               

Then on your authorization method, I’m using MongoDB for session storage:

io.set('authorization', function(handshake, callback) {
    self.setHandshakeValue( null, 'userId', null, handshake );
    if (handshake.headers.cookie) {
        var cookie = connect.utils.parseCookie(handshake.headers.cookie);
        .get(cookie['connect.sid'], function(err, session) {
            if(!err && session && session.auth && session.auth.loggedIn ) {
                self.setHandshakeValue( null,
                            handshake );

Then before saving a record in the model, you can do:

Read More:   What is the best way to implement a forced page refresh using Flask?

model._author = shished.getHandshakeValue( socket, 'userId' );

I believe checking socket.handshake should get you the session:

io.sockets.on('connection', function(socket) { 

When the client establishes a socket connection with your server, the client sends a WebSocket handshake request. What I’m doing above is grabbing the session ID from the handshake.

Assuming your code looks kinda like this:

function(client) {

The request is client.request as shown in the example above.

As a separate thing, maybe this would help:

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts