如何正确关闭node-express服务器? [英] how to properly close node-express server?

查看:279
本文介绍了如何正确关闭node-express服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ auth / github / callback
url获取回调后,我需要关闭服务器。通常 HTTP API 关闭
服务器目前正在支持 server.close([callback])
API函数,但是使用node-express服务器我得到 TypeError:Object function app(req,res){app.handle(req, RES); }没有方法'close'
错误。我不知道如何找到信息来解决这个问题。

我应该如何关闭快递服务器?



NodeJS配置说明:

  $ node --version 
v0.8.17
$ npm --version
1.2.0
$ npm视图快递版本
3.0.6

实际应用程序代码:

  var app = express(); 

// configure Express
app.configure(function(){
// ... configuration
});

app.get(
'/ auth / github / callback',
passport.authenticate('github',{failureRedirect:'/ login'}),
函数(req,res){
res.redirect('/');

setTimeout(function(){
app.close();
/ / TypeError:Object function app(req,res){app.handle(req,res);}没有方法'close'
},3000)
}
);

app.listen('http:// localhost:5000 /');

另外,我发现'nodejs express close ...'但是我不知道我是否可以使用它与我的代码: var app = express();

解决方案

app.listen()返回 http.Server 。您应该在该实例上调用 close()而不是应用程序实例。



Ex。

  app.get(
'/ auth / github / callback',
passport.authenticate('github',{failureRedirect:'/ login'}),
函数(req,res){
res.redirect('/');

setTimeout(function(){
server.close();
// ^^^^^^^^^^
},3000)
}
);

var server = app.listen('http:// localhost:5000 /');

您可以检查来源: /node_modules/express/lib/application.js


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 configuration notes:

$ 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();.

解决方案

app.listen() returns http.Server. You should invoke close() on that instance and not on app instance.

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-express服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆