最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node asynchronous route code - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论