Skip to content

Commit 89c2cef

Browse files
authored
Close server port on exit (#823)
* Close server port on exit * Set up "CTRL-C" hook, before printing out "Hit CTRL-C to stop the server"
1 parent c98ef8b commit 89c2cef

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

bin/http-server

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,27 @@ function listen(port) {
324324
}
325325
}
326326

327+
// Set up "CTRL-C" hook, before printing out "Hit CTRL-C to stop the server"
328+
function stopServer() {
329+
server.close();
330+
logger.info(chalk.red('http-server stopped.'));
331+
process.exit();
332+
}
333+
334+
process.on('SIGINT', stopServer);
335+
process.on('SIGTERM', stopServer);
336+
337+
if (process.platform === 'win32') {
338+
require('readline').createInterface({
339+
input: process.stdin,
340+
output: process.stdout
341+
}).on('SIGINT', function () {
342+
process.emit('SIGINT');
343+
});
344+
}
345+
327346
logger.info('Hit CTRL-C to stop the server');
347+
328348
if (argv.o) {
329349
let openHost = host
330350
if ('::' === host || '0.0.0.0'===host){
@@ -342,22 +362,3 @@ function listen(port) {
342362
if (!argv.s) logger.info();
343363
});
344364
}
345-
346-
if (process.platform === 'win32') {
347-
require('readline').createInterface({
348-
input: process.stdin,
349-
output: process.stdout
350-
}).on('SIGINT', function () {
351-
process.emit('SIGINT');
352-
});
353-
}
354-
355-
process.on('SIGINT', function () {
356-
logger.info(chalk.red('http-server stopped.'));
357-
process.exit();
358-
});
359-
360-
process.on('SIGTERM', function () {
361-
logger.info(chalk.red('http-server stopped.'));
362-
process.exit();
363-
});

0 commit comments

Comments
 (0)