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?
-
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 callnext()
(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 fornext
to be called? – Sebastian Nowak Commented Jul 27, 2014 at 12:52
1 Answer
Reset to default 5Express 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
评论列表(0条)