Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graceful shutdown and error handling #779

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ npm-debug.log*
.nyc_*/
.dir-locals.el
.DS_Store
.httpserver*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ This will install `http-server` globally so that it may be run from the command
|`--mimetypes` |Path to a .types file for custom mimetype definition| |
|`-h` or `--help` |Print this list and exit. | |
|`-v` or `--version`|Print the version and exit. | |
| `-n` or `--no-panic` | Don't print error stack in the console, put it in a log file | `false`|

## Magic Files

Expand Down
22 changes: 21 additions & 1 deletion bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ var chalk = require('chalk'),

fs = require('fs'),
url = require('url');

var argv = require('minimist')(process.argv.slice(2), {
alias: {
tls: 'ssl'
}
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank lines not needed



var ifaces = os.networkInterfaces();

process.title = 'http-server';
Expand Down Expand Up @@ -60,12 +64,15 @@ if (argv.h || argv.help) {
' --no-dotfiles Do not show dotfiles',
' --mimetypes Path to a .types file for custom mimetype definition',
' -h --help Print this list and exit.',
' -v --version Print the version and exit.'
' -v --version Print the version and exit.',
' --no-panic If error occurs, gracefully shut down and create log file',
' Can also be specified with the env variable NODE_HTTP_SERVER_NO_PANIC'
].join('\n'));
process.exit();
}

var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
nopanic = !argv['panic'] || argv.n || process.env.NODE_HTTP_SERVER_NO_PANIC,
host = argv.a || '0.0.0.0',
tls = argv.S || argv.tls,
sslPassphrase = process.env.NODE_HTTP_SERVER_SSL_PASSPHRASE,
Expand All @@ -75,6 +82,19 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
version = argv.v || argv.version,
logger;

if (nopanic){
process.on('error', (e)=> {
// Results in a string like "2021-12-27 14:56:31"
const etime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
console.log(colors.green(etime));
console.log(`${colors.red('Fatal error: ')}${e.code}: ${e.message}`);
const filename = `httpserver-${etime.split(' ').join('_')}.log`;
console.log(colors.bold(`Check ${filename} file in this folder.`));
fs.writeFileSync(filename, JSON.stringify(e));
process.exit(1);
});
}

var proxyOptionsBooleanProps = [
'ws', 'xfwd', 'secure', 'toProxy', 'prependPath', 'ignorePath', 'changeOrigin',
'preserveHeaderKeyCase', 'followRedirects', 'selfHandleResponse'
Expand Down
4 changes: 4 additions & 0 deletions doc/http-server.1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Default file extension is none is provided.
.BI \-s ", " \-\-silent
Suppress log messages from output.

.TP
.BI \-n ", " \-\-no-panic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the -n needs removing

Gracefully shut down whenever a fatal error occurs, sending stack to log file, not console.

.TP
.BI \-\-cors " " [\fIHEADERS\fR]
Enable CORS via the "Access-Control-Allow-Origin" header.
Expand Down