javascript - Node asynchronous route code - Stack Overflow

I'm using node with express 4.0. I can't find anything on the internet (including the docs) a

I'm using node with express 4.0. I can't find anything on the internet (including the docs) about embedding asynchronous code in a route.

With a middleware it's quite simple:

app.use('/something', function (req, res, next)
{
  doSomethingAsync(function(err, probablySomethingElse)
  {
    // probably some error checking
    next();
  });
});

The problem with routes is that there is no next callback, so how does express know when to move to the next job?

app.get('/something', function (req, res)
{
  res.render('someTemplate');
  // no next() here, but it still works
});

If I had to guess, I'd say that express moves to the next task right after the above function exits. But out of curiosity I've launched the following code...

app.get('/something', function (req, res, next)
{
  console.log(next);
});

...and there actually is some next callback passed. So what's going on here? How does it work behind the scenes? And how can I put asynchronous code there?

I'm using node with express 4.0. I can't find anything on the internet (including the docs) about embedding asynchronous code in a route.

With a middleware it's quite simple:

app.use('/something', function (req, res, next)
{
  doSomethingAsync(function(err, probablySomethingElse)
  {
    // probably some error checking
    next();
  });
});

The problem with routes is that there is no next callback, so how does express know when to move to the next job?

app.get('/something', function (req, res)
{
  res.render('someTemplate');
  // no next() here, but it still works
});

If I had to guess, I'd say that express moves to the next task right after the above function exits. But out of curiosity I've launched the following code...

app.get('/something', function (req, res, next)
{
  console.log(next);
});

...and there actually is some next callback passed. So what's going on here? How does it work behind the scenes? And how can I put asynchronous code there?

Share Improve this question asked Jul 27, 2014 at 12:33 Sebastian NowakSebastian Nowak 5,7179 gold badges70 silver badges111 bronze badges 3
  • Could you be a little clearer, what async code do you want to put in the route, and how is that related to the next callback, which all routes happen to have. – adeneo Commented Jul 27, 2014 at 12:35
  • 1 When you res.render() something, you don't need to call next() (even if it is passed so that you could if you wanted to) because it's the end of the chain. – Bergi Commented Jul 27, 2014 at 12:42
  • Express somehow needs to know when my callback exits so it can close the socket and drop the data associated with this request. Do I understand correctly, that if I call res.render it will asume that there are no async tasks scheduled and it doesn't have to wait for next to be called? – Sebastian Nowak Commented Jul 27, 2014 at 12:52
Add a ment  | 

1 Answer 1

Reset to default 5

Express will wait until you call res.render to close the socket. Which means that you can pass res.render into a callback that takes X secs to execute and everything will still work.

The next allows you to go to the next route that maps your value, you can find a very good explanation here: What is the parameter "next" used for in Express?

But under what you are asking here. The moment render() is called on the res object, then data will be sent and the socket closed.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745421029a4626963.html

相关推荐

  • javascript - Node asynchronous route code - Stack Overflow

    I'm using node with express 4.0. I can't find anything on the internet (including the docs) a

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信