Skip to content

add super verbose, show unknown income request header and body #493

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

Closed
wants to merge 7 commits into from
Closed
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
3 changes: 3 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

`-V` or `--superverbose` superverbose mode ,can display a unknown request header and body. such as an web hook request, but no detail api document.

`--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 All @@ -75,6 +77,7 @@ Using `npx` you can run the script without installing it first:

`-r` or `--robots` Provide a /robots.txt (whose content defaults to `User-agent: *\nDisallow: /`)


`--no-dotfiles` Do not show dotfiles

`-h` or `--help` Print this list and exit.
Expand Down
46 changes: 46 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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',
' -V --superverbose superverbose mode, display request headers and body',
' --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 All @@ -52,10 +53,12 @@ if (argv.h || argv.help) {
' -C --cert Path to ssl cert file (default: cert.pem).',
' -K --key Path to ssl key file (default: key.pem).',
'',

' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
' -h --help Print this list and exit.',
' -v --version Print the version and exit.'

].join('\n'));
process.exit();
}
Expand All @@ -65,6 +68,7 @@ var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
ssl = argv.S || argv.ssl,
proxy = argv.P || argv.proxy,
utc = argv.U || argv.utc,
superverbose = argv.V || argv.superverbose,
version = argv.v || argv.version,
logger;

Expand Down Expand Up @@ -99,6 +103,48 @@ else if (colors) {
request: function () {}
};
}
if (superverbose) {
if (argv.s || argv.silent) {
console.log(colors.red('superverbose and silent mode can not used together! pick one of two'));
process.exit(1);
}
logger = {
info: console.log,
request: function (req, res, error) {
var date = utc ? new Date().toUTCString() : new Date();
if (error) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we be showing all this info if there was an error, or if there was no error? Was the logic accidentally flipped here?

logger.info(
'[%s] "%s %s" Error (%s): "%s"',
date, colors.red(req.method), colors.red(req.url),
colors.red(error.status.toString()), colors.red(error.message)
);
}
logger.info('---------------headers-------------');
var request = req.request;
var rawHeaders = request.rawHeaders;
if (rawHeaders && rawHeaders.length) {
var headerStr = rawHeaders.reduce(function (u, v, i) {
if (i === 1) {
return u + ': ' + v + '\n';
}
var append = i % 2 === 0 ? ': ' : '\n' ;
return u + v + append ;
});
logger.info(colors.cyan(headerStr));
}
if (req.readable) {
var body = [];
req.request.on('data', function (chunk) {
body.push(chunk);
}).on('end', function () {
body = Buffer.concat(body).toString();
logger.info('-------------request body----------');
logger.info(body) ;
});
}
}
};
}

if (version) {
logger.info('v' + require('../package.json').version);
Expand Down