javascript - 如果我在 express.js 中省略 next() 会怎样?

标签 javascript node.js express

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/

相关文章:

node.js - 如何使用 node.js 发送 HTTP/2.0 请求

node.js - 内容类型为 : multipart/form-data Troubleshooting 的 NodeJS Post 请求

javascript - 在一个文件中要求express并在另一个文件中使用express.static

node.js - 在 Node.js 中如何从处理程序/回调方法向客户端发送消息

node.js - Node JS : Uncork() method on writable stream doesn't really flush the data

javascript - 如果出现 404,为什么网页会永远加载?

javascript - 帮助我了解变量设置

javascript - DOM MutationObserver 问题 - Javascript

javascript - 尽管 ng Include angularjs 不会显示 html 父内容

javascript - 如何使用 Ajax 调用 JavaScript 文件并使用该 JavaScript 文件中的逻辑