Manage each request session in node js to authentication or anything

In this post, I’m using node-continuation-local-storage  to manage session per each request to server node js.

Step1: Install it

Step2: Init session in middleware in route of node js

public static authentication(req: express.Request, res: express.Response, next : Function) {
    let token = req.headers['x-access-token'];
    // decode token
    if (token) {
        // verifies secret and checks exp
       => {
                    session.set(UtilHelper.USER_ID, user.user_id);

    } else {


Step3: To get and set variable more:

public static getSession(key: string): string {
    let session = require('continuation-local-storage').getNamespace(this.SESSION_NAMESPACE);
    if (session != undefined) {
        return session.get(key);
    return '';

public static setSession(key: string, value: any): void {
    var session = require('continuation-local-storage').createNamespace(this.SESSION_NAMESPACE);
    if (session != undefined) {
        session.set(key, value);

Hope to help you something!






Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s