Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Using `npx` you can run the script without installing it first:

`-s` or `--silent` Suppress log messages from output

`--nv` or `--no-verbose` Only show the basic info

`--cors` Enable CORS via the `Access-Control-Allow-Origin` header

`-o [path]` Open browser window after starting the server. Optionally provide a URL path to open. e.g.: -o /other/dir/
Expand Down
54 changes: 32 additions & 22 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var colors = require('colors/safe'),
argv = require('optimist')
.boolean('cors')
.boolean('log-ip')
.default('verbose', true)
.argv;

var ifaces = os.networkInterfaces();
Expand All @@ -31,6 +32,7 @@ if (argv.h || argv.help) {
' If both brotli and gzip are enabled, brotli takes precedence',
' -e --ext Default file extension if none supplied [none]',
' -s --silent Suppress log messages from output',
' --nv --no-verbose Only show the basic info',
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
' Optionally provide CORS headers list separated by commas',
' -o [path] Open browser window after starting the server.',
Expand Down Expand Up @@ -68,29 +70,37 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
logger;

if (!argv.s && !argv.silent) {
logger = {
info: console.log,
request: function (req, res, error) {
var date = utc ? new Date().toUTCString() : new Date();
var ip = argv['log-ip']
? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress
: '';
if (error) {
logger.info(
'[%s] %s "%s %s" Error (%s): "%s"',
date, ip, colors.red(req.method), colors.red(req.url),
colors.red(error.status.toString()), colors.red(error.message)
);
}
else {
logger.info(
'[%s] %s "%s %s" "%s"',
date, ip, colors.cyan(req.method), colors.cyan(req.url),
req.headers['user-agent']
);
if (argv.nv || !argv.verbose) {
logger = {
info: console.log,
request: function () {}
};
}
else {
logger = {
info: console.log,
request: function (req, res, error) {
var date = utc ? new Date().toUTCString() : new Date();
var ip = argv['log-ip']
? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress
: '';
if (error) {
logger.info(
'[%s] %s "%s %s" Error (%s): "%s"',
date, ip, colors.red(req.method), colors.red(req.url),
colors.red(error.status.toString()), colors.red(error.message)
);
}
else {
logger.info(
'[%s] %s "%s %s" "%s"',
date, ip, colors.cyan(req.method), colors.cyan(req.url),
req.headers['user-agent']
);
}
}
}
};
};
}
}
else if (colors) {
logger = {
Expand Down