-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add graceful shutdown and error handling #779
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
base: master
Are you sure you want to change the base?
Changes from 13 commits
6798d53
2a17a34
e2842ad
155be49
160950c
372bc0e
e24051e
ed2787e
c5d5eef
9835f98
31f6fcc
c11b18b
a470760
a2d9719
f9d1dea
2d65af8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ npm-debug.log* | |
.nyc_*/ | ||
.dir-locals.el | ||
.DS_Store | ||
.httpserver* |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,15 @@ var colors = require('colors/safe'), | |
|
||
fs = require('fs'), | ||
url = require('url'); | ||
|
||
var argv = require('minimist')(process.argv.slice(2), { | ||
alias: { | ||
tls: 'ssl' | ||
} | ||
}); | ||
|
||
|
||
|
||
var ifaces = os.networkInterfaces(); | ||
|
||
process.title = 'http-server'; | ||
|
@@ -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.', | ||
' -n --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, | ||
|
@@ -75,6 +82,22 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10), | |
version = argv.v || argv.version, | ||
logger; | ||
|
||
|
||
if (nopanic){ | ||
|
||
process.on('error', (e)=> { | ||
let etime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') | ||
console.log(colors.green(etime)) | ||
console.log(colors.red("Fatal error: ")+ e.code + ": "+e.message) | ||
let fname = ".httpserver-"+etime.split(" ")[0]+"-"+etime.split(" ")[1] | ||
|
||
console.log(colors.bold(`Check ${fname} file in this folder.`)) | ||
fs.writeFileSync(fname, JSON.stringify(e)) | ||
process.exit(1) | ||
}); | ||
|
||
} | ||
|
||
|
||
Octalbyte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var proxyOptionsBooleanProps = [ | ||
'ws', 'xfwd', 'secure', 'toProxy', 'prependPath', 'ignorePath', 'changeOrigin', | ||
'preserveHeaderKeyCase', 'followRedirects', 'selfHandleResponse' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,10 @@ Default file extension is none is provided. | |
.BI \-s ", " \-\-silent | ||
Suppress log messages from output. | ||
|
||
.TP | ||
.BI \-n ", " \-\-no-panic | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank lines not needed