Skip to content

Commit 393e010

Browse files
OctalbytethornjadKernelDeimos
authored
Add graceful shutdown and error handling (#779)
* Add graceful shutdown and error handling * Add to --help switch and turn into optional (ENV variables)' * Add readme documentation * Change documentation inside --help switch * Add some tests and debugging (commented out). Currently not functional due to switch not working * Add documentation to http-server.1 file * -n switch * Fixes, commented out tests * - * Delte log files * Add log files to gitignore * Comment out all tests * Delete tests * Fixes (according to suggestion) Co-authored-by: Jade Michael Thornton <[email protected]> * Remove `-n` short argument and enhance the logfile name * Apply suggestion from @KernelDeimos * Apply suggestion from @KernelDeimos --------- Co-authored-by: Octopus <[email protected]> Co-authored-by: Jade Michael Thornton <[email protected]> Co-authored-by: Eric Dubé <[email protected]>
1 parent e3e4241 commit 393e010

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ npm-debug.log*
33
.nyc_*/
44
.dir-locals.el
55
.DS_Store
6+
.httpserver*
67
.tap

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ with the provided Dockerfile.
9696
|`--mimetypes` |Path to a .types file for custom mimetype definition| |
9797
|`-h` or `--help` |Print this list and exit. | |
9898
|`-v` or `--version`|Print the version and exit. | |
99+
| `--no-panic` | Don't print error stack in the console, put it in a log file | `false`|
99100

100101
## Magic Files
101102

bin/http-server

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var chalk = require('chalk'),
77
httpServer = require('../lib/http-server'),
88
portfinder = require('portfinder'),
99
opener = require('opener'),
10-
1110
fs = require('fs'),
1211
url = require('url');
1312
var argv = require('minimist')(process.argv.slice(2), {
@@ -18,7 +17,6 @@ var argv = require('minimist')(process.argv.slice(2), {
1817
},
1918
boolean: ['proxy-all']
2019
});
21-
2220
var ifaces = os.networkInterfaces();
2321

2422
if (argv.h || argv.help) {
@@ -86,12 +84,15 @@ if (argv.h || argv.help) {
8684
' --no-dotfiles Do not show dotfiles',
8785
' --mimetypes Path to a .types file for custom mimetype definition',
8886
' -h --help Print this list and exit.',
89-
' -v --version Print the version and exit.'
87+
' -v --version Print the version and exit.',
88+
' --no-panic If error occurs, gracefully shut down and create log file',
89+
' Can also be specified with the env variable NODE_HTTP_SERVER_NO_PANIC'
9090
].join('\n'));
9191
process.exit();
9292
}
9393

9494
var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
95+
nopanic = !argv['panic'] || argv.n || process.env.NODE_HTTP_SERVER_NO_PANIC,
9596
host = argv.a || '::',
9697
tls = argv.S || argv.tls,
9798
title = argv.T || argv.title,
@@ -106,6 +107,19 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
106107
baseDir = argv['base-dir'],
107108
logger;
108109

110+
if (nopanic){
111+
process.on('error', (e)=> {
112+
// Results in a string like "2021-12-27 14:56:31"
113+
const etime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
114+
console.log(colors.green(etime));
115+
console.log(`${colors.red('Fatal error: ')}${e.code}: ${e.message}`);
116+
const filename = `httpserver-${etime.split(' ').join('_')}.log`;
117+
console.log(colors.bold(`Check ${filename} file in this folder.`));
118+
fs.writeFileSync(filename, JSON.stringify(e));
119+
process.exit(1);
120+
});
121+
}
122+
109123
var proxyOptionsBooleanProps = [
110124
'ws', 'xfwd', 'secure', 'toProxy', 'prependPath', 'ignorePath', 'changeOrigin',
111125
'preserveHeaderKeyCase', 'followRedirects', 'selfHandleResponse'

doc/http-server.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ Default file extension is none is provided.
6969
.BI \-s ", " \-\-silent
7070
Suppress log messages from output.
7171

72+
.TP
73+
.BI \-n ", " \-\-no-panic
74+
Gracefully shut down whenever a fatal error occurs, sending stack to log file, not console.
75+
7276
.TP
7377
.BI \-\-coop " " [\fIMODE\fR]
7478
Enable COOP via the "Cross-Origin-Opener-Policy" header and sets

0 commit comments

Comments
 (0)