如何正确关闭node-express服务器? [英] how to properly close node-express server?
问题描述
我需要从 /auth/github/callback
得到回调后关闭服务器网址.通常 HTTP API 关闭服务器目前支持 server.close([callback])
API 函数,但是使用 node-express 服务器我得到 TypeError: Object function app(req, res){ app.handle(req, res);} 没有方法关闭"
错误.而且我不知道如何查找信息来解决这个问题.
我应该如何关闭快递服务器?
I need to close server after getting callback from /auth/github/callback
url. With usual HTTP API closing
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?
NodeJS 配置注意事项:
NodeJS configuration notes:
$ node --version
v0.8.17
$ npm --version
1.2.0
$ npm view express version
3.0.6
实际应用代码:
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/');
另外,我发现 'nodejs express close...' 但我不确定我是否可以将它与我拥有的代码一起使用:var app = express();
.
Also, I have found ‘nodejs express close…’ but I don't sure if I can use it with code I have: var app = express();
.
推荐答案
app.listen()
返回 http.Server
.您应该在该实例上调用 close()
而不是在 app
实例上调用.
app.listen()
returns http.Server
. You should invoke close()
on that instance and not on app
instance.
例如
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/');
您可以检查源代码:/node_modules/express/lib/application.js
这篇关于如何正确关闭node-express服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!