노드 익스프레스 서버를 올바르게 닫는 방법은 무엇입니까?
/auth/github/callback
URL 에서 콜백을받은 후 서버를 닫아야합니다 . 일반적인 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
'programing tip' 카테고리의 다른 글
ElasticSearch — 필드 값을 기반으로 관련성 향상 (0) | 2020.11.25 |
---|---|
Bitbucket에서 태그를 추가하는 방법은 무엇입니까? (0) | 2020.11.24 |
간단한 프로그램을위한 클래스 로딩의 흐름 (0) | 2020.11.24 |
입력 유형 "숫자"는 크기가 조정되지 않습니다. (0) | 2020.11.24 |
Ansible을 사용한 SSH 에이전트 전달 (0) | 2020.11.24 |