express.js 文档指出,如果 next()
从未在路由中调用,则
the request will be left hanging
( https://expressjs.com/en/guide/writing-middleware.html )
这意味着什么?这是否意味着我应该使用 next()
完成每个请求,即使我已经调用了 res.send
并且请求处理已完成?省略了会不会有内存泄露等问题?
更新: 我确实混淆了中间件和路由。我忘记了循环可以结束。但我不确定如何结束它。
最佳答案
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?
整个句子是...
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.
因此,如果您打算结束当前中间件内的循环,您可以只调用 res.send、res.end 或 res.json 而无需调用 next()。但是如果你打算去下一个中间件,你应该调用 next() 否则它不会去下一个中间件。
Won't there be memory leaks or other problems if I do omit it?
如果请求/响应周期没有完成,那么分配给那个周期的内存将不会被归还,这意味着内存泄漏。
关于javascript - 如果我在 express.js 中省略 next() 会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46666636/