how to properly close node-express server?

前端 未结 4 1233
孤街浪徒
孤街浪徒 2020-12-08 01:35

I need to close server after getting callback from /auth/github/callback url. With usual HTTP API closing server is currently supporting with s

4条回答
  •  离开以前
    2020-12-08 02:22

    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

提交回复
热议问题