Javascript 如何正确关闭node-express服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14515954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to properly close node-express server?
提问by Vladimir Starkov
I need to close server after getting callback from /auth/github/callbackurl. With usual HTTP APIclosing
server is currently supporting with server.close([callback])API function, but with node-express server i'm getting TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'close'error. And I don't know how to find information to solve this problem.
How should I close express server?
从/auth/github/callbackurl获取回调后,我需要关闭服务器。使用通常的HTTP API关闭服务器当前支持server.close([callback])API 功能,但是使用 node-express 服务器我收到TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'close'错误。而且我不知道如何查找信息来解决这个问题。
我应该如何关闭快递服务器?
NodeJS configuration notes:
NodeJS 配置注意事项:
$ node --version
v0.8.17
$ npm --version
1.2.0
$ npm view express version
3.0.6
Actual application code:
实际应用代码:
var app = express();
// configure Express
app.configure(function() {
// … configuration
});
app.get(
'/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
setTimeout(function () {
app.close();
// TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'close'
}, 3000)
}
);
app.listen('http://localhost:5000/');
Also, I have found ‘nodejs express close…'but I don't sure if I can use it with code I have: var app = express();.
另外,我发现“表达的NodeJS接近......”但我不知道我是否可以用代码中使用它,我有:var app = express();。
回答by Vladimir Kuznetsov
app.listen()returns http.Server. You should invoke close()on that instance and not on appinstance.
app.listen()返回http.Server。您应该close()在该实例上而不是在app实例上调用。
Ex.
前任。
app.get(
'/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
setTimeout(function () {
server.close();
// ^^^^^^^^^^^
}, 3000)
}
);
var server = app.listen('http://localhost:5000/');
You can inspect sources: /node_modules/express/lib/application.js
您可以检查来源: /node_modules/express/lib/application.js
回答by Michael
In express v3 they removed this function.
在 express v3 中,他们删除了这个功能。
You can still achieve the same by assigning the result of app.listen()function and apply close on it:
您仍然可以通过分配app.listen()函数的结果并对其应用关闭来实现相同的效果:
var server = app.listen(3000);
server.close()
回答by Neha Sharma
If any error occurs in your express app then you must have to close the server and you can do that like below-
如果您的 express 应用程序中发生任何错误,那么您必须关闭服务器,您可以像下面那样执行此操作-
var app = express();
var server = app.listen(process.env.PORT || 5000)
If any error occurs then our application will get a signal named SIGTERM
如果发生任何错误,那么我们的应用程序将获得一个名为 SIGTERM
You can read more about node signal here-
您可以在此处阅读有关节点信号的更多信息-
https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
process.on('SIGTERM', () => {
console.info('SIGTERM signal received.');
console.log('Closing http server.');
server.close(() => {
console.log('Http server closed.');
});
});
回答by Gajus
I have answered a variation of "how to terminate a HTTP server" many times on different node.jssupport channels. Unfortunately, I couldn't recommend any of the existing libraries because they are lackingin one or another way. I have since put together a package that (I believe) is handling all the cases expected of graceful express.js HTTP(S) server termination.
我已经在不同的node.js支持频道上多次回答了“如何终止 HTTP 服务器”的变体。不幸的是,我不能推荐任何现有的库,因为它们以一种或另一种方式缺乏。从那以后,我整理了一个包(我相信)可以处理优雅的 express.js HTTP(S) 服务器终止所期望的所有情况。
https://github.com/gajus/http-terminator
https://github.com/gajus/http-terminator
The main benefit of http-terminatoris that:
http-terminator的主要好处是:
- it does not monkey-patch Node.js API
- it immediately destroys all sockets without an attached HTTP request
- it allows graceful timeout to sockets with ongoing HTTP requests
- it properly handles HTTPS connections
- it informs connections using keep-alive that server is shutting down by setting a connection: close header
- it does not terminate the Node.js process
- 它不会修补 Node.js API
- 它立即销毁所有没有附加 HTTP 请求的套接字
- 它允许对正在进行的 HTTP 请求的套接字进行正常超时
- 它正确处理 HTTPS 连接
- 它通过设置连接通知使用 keep-alive 服务器正在关闭的连接:关闭标头
- 它不会终止 Node.js 进程

