diff --git a/bin/http-server b/bin/http-server index 7c597fa8..bec5a443 100755 --- a/bin/http-server +++ b/bin/http-server @@ -242,7 +242,27 @@ function listen(port) { } } + // Set up "CTRL-C" hook, before printing out "Hit CTRL-C to stop the server" + function stopServer() { + server.close(); + logger.info(chalk.red('http-server stopped.')); + process.exit(); + } + + process.on('SIGINT', stopServer); + process.on('SIGTERM', stopServer); + + if (process.platform === 'win32') { + require('readline').createInterface({ + input: process.stdin, + output: process.stdout + }).on('SIGINT', function () { + process.emit('SIGINT'); + }); + } + logger.info('Hit CTRL-C to stop the server'); + if (argv.o) { const openHost = host === '0.0.0.0' ? '127.0.0.1' : host; let openUrl = `${protocol}${openHost}:${port}`; @@ -257,22 +277,3 @@ function listen(port) { if (!argv.s) logger.info(); }); } - -if (process.platform === 'win32') { - require('readline').createInterface({ - input: process.stdin, - output: process.stdout - }).on('SIGINT', function () { - process.emit('SIGINT'); - }); -} - -process.on('SIGINT', function () { - logger.info(chalk.red('http-server stopped.')); - process.exit(); -}); - -process.on('SIGTERM', function () { - logger.info(chalk.red('http-server stopped.')); - process.exit(); -});