From 684caf369020a3d750f52a9d153e9bfb99133e5c Mon Sep 17 00:00:00 2001 From: Ngoc Dao Date: Thu, 23 Jun 2022 10:19:29 +1000 Subject: [PATCH 1/2] Close server port on exit --- bin/http-server | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/bin/http-server b/bin/http-server index 7c597fa8..b3030fed 100755 --- a/bin/http-server +++ b/bin/http-server @@ -255,24 +255,23 @@ function listen(port) { // Spacing before logs if (!argv.s) logger.info(); - }); -} -if (process.platform === 'win32') { - require('readline').createInterface({ - input: process.stdin, - output: process.stdout - }).on('SIGINT', function () { - process.emit('SIGINT'); - }); -} + function stopServer() { + server.close(); + logger.info(chalk.red('http-server stopped.')); + process.exit(); + } -process.on('SIGINT', function () { - logger.info(chalk.red('http-server stopped.')); - process.exit(); -}); + process.on('SIGINT', stopServer); + process.on('SIGTERM', stopServer); -process.on('SIGTERM', function () { - logger.info(chalk.red('http-server stopped.')); - process.exit(); -}); + if (process.platform === 'win32') { + require('readline').createInterface({ + input: process.stdin, + output: process.stdout + }).on('SIGINT', function () { + process.emit('SIGINT'); + }); + } + }); +} From aeb5790136f2bc02cb16ef7c6076cfa1884989ce Mon Sep 17 00:00:00 2001 From: Ngoc Dao Date: Fri, 24 Jun 2022 11:46:40 +1000 Subject: [PATCH 2/2] Set up "CTRL-C" hook, before printing out "Hit CTRL-C to stop the server" --- bin/http-server | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/bin/http-server b/bin/http-server index b3030fed..bec5a443 100755 --- a/bin/http-server +++ b/bin/http-server @@ -242,20 +242,7 @@ function listen(port) { } } - 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}`; - if (typeof argv.o === 'string') { - openUrl += argv.o[0] === '/' ? argv.o : '/' + argv.o; - } - logger.info('Open: ' + openUrl); - opener(openUrl); - } - - // Spacing before logs - if (!argv.s) logger.info(); - + // 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.')); @@ -273,5 +260,20 @@ function listen(port) { 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}`; + if (typeof argv.o === 'string') { + openUrl += argv.o[0] === '/' ? argv.o : '/' + argv.o; + } + logger.info('Open: ' + openUrl); + opener(openUrl); + } + + // Spacing before logs + if (!argv.s) logger.info(); }); }