programing tip

노드 익스프레스 서버를 올바르게 닫는 방법은 무엇입니까?

itbloger 2020. 11. 24. 07:45
반응형

노드 익스프레스 서버를 올바르게 닫는 방법은 무엇입니까?


/auth/github/callbackURL 에서 콜백을받은 후 서버를 닫아야합니다 . 일반적인 HTTP API 폐쇄 서버는 현재 server.close([callback])API 기능을 지원 하지만 노드 익스프레스 서버에서는 TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'close'오류 발생합니다. 그리고 나는이 문제를 해결하기위한 정보를 찾는 방법을 모릅니다.
Express Server를 닫으려면 어떻게해야합니까?

NodeJS 구성 참고 사항 :

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


app.listen()를 반환합니다 http.Server. close()인스턴스가 아닌 해당 인스턴스에서 호출해야 app합니다.

전의.

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


Express v3에서는이 기능을 제거했습니다.

You can still achieve the same by assigning the result of app.listen() function and apply close on it:

var server = app.listen(3000);
server.close()

https://github.com/visionmedia/express/issues/1366


If any error occurs in your express app then you must have to close the server and you can do that like below-

var app = express();
var server = app.listen(process.env.PORT || 5000)

If any error occurs then our application will get a signal named SIGTERM

You can read more about node signal here-

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.');
  });
});

참고URL : https://stackoverflow.com/questions/14515954/how-to-properly-close-node-express-server

반응형