diff --git a/README.md b/README.md index 968c53e6..5e0e0c46 100644 --- a/README.md +++ b/README.md @@ -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/ @@ -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. diff --git a/bin/http-server b/bin/http-server index 895e1910..ba884da5 100755 --- a/bin/http-server +++ b/bin/http-server @@ -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.', @@ -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(); } @@ -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; @@ -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) { + 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);