Reputation: 20565
I am trying to create a close event that fires before the server shuts down.
for this ive created the following:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var listenTo = require('./config/port.json')["port"];
http.listen(listenTo, function () {
console.log('listening on *:' + listenTo);
});
http.on('close', function (event) {
console.log('closed');
});
However this just shuts down the server.
So my question is how can i listen on server shut down using http
Upvotes: 0
Views: 900
Reputation: 111506
Instead of listening for the event, you are closing the server.
Instead of this:
http.close(function (event) {
console.log('closed');
});
you should use this:
http.on('close', function (event) {
console.log('closed');
});
Compare this:
with this:
Note that if you want to handle events of closing the server application (terminating the process) and not just closing the http
server (closing the open port) then you need to handle a different kind of events, like:
process.on('SIGINT', function () {
console.log('SIGINT caught');
// if you want to exit then call:
process.exit();
});
Upvotes: 3