简体   繁体   中英

What if I omit next() in express.js?

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.

What 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.

我认为这不会导致任何内存泄漏,我们也可以使用next()来调用下一个middleware假设您有两件事要做,首先验证用户身份,然后发送该请求的数据,现在您应该做的是首先验证身份在一个中间件中,然后在用户成功验证该请求时调用下一个,否则无法调用。

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 (eg 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
})

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM