node-continuation-local-storage icon indicating copy to clipboard operation
node-continuation-local-storage copied to clipboard

Context gets lost in promise when deployed as a serverless app

Open rajasekhar911 opened this issue 7 years ago • 2 comments

I have a NodeJS application using Node8 and Express 4. Am using CLS to store an API token at the time of login and to retrieve the token further down the line. This token is in session and is set to CLS on every request (so that I dont have to pass the request object to each and every method) so that I can retrieve it at a later time in the nth function.

app.use(function(req, res, next) {
  var getNamespace = require('continuation-local-storage').getNamespace,
  authSession = getNamespace('authSession');
  var token = "";
  if(req.session && req.session.auth){
    token = req.session.auth.token;
  }
  authSession.bindEmitter(req);
  authSession.bindEmitter(res);

  authSession.run(function() {
    authSession.set('authToken', token);
    next();
  });
});

This is working fine as a node js app. But when I try to run it as a serverless app, the context does not seem to be set properly. Am using serverless-http package.

I tried to put the patch mentioned in issue 116 ( [https://github.com/othiym23/node-continuation-local-storage/issues/116] ). But the interesting part is context.res is undefined in the finally block.

How can I proceed fixing this?

rajasekhar911 avatar Jun 01 '18 08:06 rajasekhar911

Did you find a way forward?

patran avatar Jul 12 '19 15:07 patran

check out this commit.. https://github.com/rajasekhar911/node-continuation-local-storage/commit/ca6fede72c3da4da68582912e485cc674552407d

rajasekhar911 avatar Jul 18 '19 02:07 rajasekhar911