The express.js docs state that if next()
is never called in route then
the request will be left hanging
(.html)
What does it imply? Does it mean that I should finalize every request with next()
even if I've already called res.send
and the request handling is finished? Won't there be memory leaks or other problems if I do omit it?
Update: I have indeed confused middleware with routes. And I forgot that the cycle can be ended. But I was not sure how to end it.
The express.js docs state that if next()
is never called in route then
the request will be left hanging
(https://expressjs.com/en/guide/writing-middleware.html)
What does it imply? Does it mean that I should finalize every request with next()
even if I've already called res.send
and the request handling is finished? Won't there be memory leaks or other problems if I do omit it?
Update: I have indeed confused middleware with routes. And I forgot that the cycle can be ended. But I was not sure how to end it.
Share Improve this question edited Oct 10, 2017 at 14:44 Gherman asked Oct 10, 2017 at 12:20 GhermanGherman 7,42612 gold badges53 silver badges79 bronze badges 2- It says so for the middleware, but you're asking about routing. Are you sure you're reading the correct part of docs? Do you know what middleware is? – Mjh Commented Oct 10, 2017 at 12:24
- Post your code to better explain the problem – Basheer Kharoti Commented Oct 10, 2017 at 12:45
5 Answers
Reset to default 7What does it imply?
It means the request/response cycle is still not finished. The response will not be sent to the browser and the browser will keep waiting.
Does it mean that I should finalize every request with next() even if I've already called res.send and the request handling is finished?
The whole sentence is...
If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.
So if you intend to end the cycle within the current middleware you can just call res.send, res.end or res.json without next(). But if you intend to go to the next middleware, you should call next() otherwise it won't go to the next middleware.
Won't there be memory leaks or other problems if I do omit it?
If the request/response cycle is not finished, then the memory allocated to serve that cycle will not be returned, which means memory leaks.
No memory leak, and you do not want to call next()
if there is no more middleware to run(after res.send()
). next()
is a placeholder for a cb function. That is what middleware is, one or more functions called sequentially, until reaching the end of the request-response cycle.In this example, when we hit the login
endpoint we will run 2 pieces of middleware: validate
and changeStuff
, then we call the returnResponse
function and that ends the request-response cycle.
get('/login',
validate,
changeStuff,
returnResponse
);
function validate(req, res, next) {
//validate logic does something then calls next()
next()
//just like calling changeStuff(req, res, next)
}
function changeStuff(req, res, next) {
//changeStuff logic changes something then calls next()
next()
//just like calling returnResponse(req, res, next)
}
function returnResponse(req, res) {
//will return something and that is the end of the req-res cycle
//there are no more functions to call, if you try to call next()
//you would get an error, because at this point next() would be
//undefined
res.send(req.body)
}
The next
or callback
function is essentially way of signalling the event loop emitter that the current function is done processing. You can pass data to next function by passing it as param to it.
If you don't call callback
or next
function, the functions which are next in event loop, will never be executed. Express must be having certain functions chain after processing a request. So, definitely not a good practice to skip calling the callback function itself.
next()
is called when you want to pass the control to the next middleware in the chain (e.g. a generic middleware to handle errors)
since the order declaration of the middleware is relevant, express will just take care of verify the arguments of your function, if it finds a third parameter then it will pass a pointer to to the subsequent middleware.
if you return the result to the client (eg. calling res.send()
) you don't need the next()
to be called since the request is already ended.
//this is an example of a hanging request since you're not calling
//niether next nor res.send()
router.get('/myRoute', (req, res, next) => {
//do nothing
})
I don't think it will cause any memory leaks also we use next()
in order to call next middleware
Like suppose you have two things to do first authenticate the user and then send data for that request now what you should do is first authenticate in one middleware and then call next when user authenticate that request successfully otherwise not.