Gracefully shutdown a server upon receiving the specified signal(s)
Shuts down a
Server gracefully upon the first specified signal received by the process and executes the optional callback.
var gracefullyShutdown = require'graceful-shutdown';var server = httpcreateServer;gracefullyShutdownserverupon'SIGINT SIGTERM'on'shutting-down'console.log'server#close() has been called';;
graceful-shutdown exports a single constructor. It may be called with or without the
var GracefulShutdown = require'graceful-shutdown';// these are the same:var gs = GracefulShutdownserver;var gs = server;
The constructor accepts a single
Accepts either a space delimited list of events or an array of event names. Listeners for these events will be added to the
process. Once any one of these events are emitted on the
server.close() will be executed, and all registered event listeners will be removed.
// these are the samevar gs = GracefulShutdownserverupon'SIGTERM SIGINT';var gs = GracefulShutdownserverupon'SIGTERM' 'SIGINT';
The GracefulShutdown instance will emit a
shutting-down event when
server.close() has been called.
Note, this is not a listener for the server close event. If you want to listen for the server
close event you must add the listener yourself.
GracefulShutdownserverupon'SIGTERM SIGINT'on'shutting-down'console.log'server.close() has been called';;serveron'close' console.logbindconsole 'the server is closed';
An alternative way to register a callback for the
shutting-down event is to pass it as the second argument to GracefulShutdown.
GracefulShutdownserver callbackupon'SITERM SIGINT';
npm install graceful-shutdown