Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to req, res objects #135

Open
samuelbodin opened this issue Aug 2, 2018 · 2 comments
Open

Access to req, res objects #135

samuelbodin opened this issue Aug 2, 2018 · 2 comments

Comments

@samuelbodin
Copy link

samuelbodin commented Aug 2, 2018

From the examples it's not entirely clear to me how I can access data that I pass to the bindEmitter() function. In the code below I use the createServer from the http module. Now my question is, how can I save the request and response objects to later access them from my foo function?

var createNamespace = require('continuation-local-storage').createNamespace;
var session  = createNamespace('some namespace');
var http = require('http');

function foo() {
    // How do I access res/req from here?
}

http.createServer(function (req, res) {
  session.bindEmitter(req);
  session.bindEmitter(res);

  // Call foo without passing in req and res.
  foo();
}).listen(8000);
@carlisliu
Copy link

var createNamespace = require('continuation-local-storage').createNamespace;
var session  = createNamespace('some namespace');
var http = require('http');


http.createServer(function (req, res) {
  session.bindEmitter(req);
  session.bindEmitter(res);

  // Call foo without passing in req and res.
  foo();

  function foo() {
      console.log(req, res);
  }
}).listen(8000);

like this?

@Qard
Copy link
Collaborator

Qard commented Aug 8, 2018

bindEmitter only ensures continuation through event emitters. It doesn't expose anything to the context.

What you need is to do something like session.set('request', req) and then session.get('request') later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants