Skip to content

Instantly share code, notes, and snippets.

@kuhnza
Last active May 25, 2024 05:01
Show Gist options
  • Save kuhnza/5202184 to your computer and use it in GitHub Desktop.
Save kuhnza/5202184 to your computer and use it in GitHub Desktop.
A simple node server that dumps raw incoming http requests out to the command line.
var http = require('http');
var LISTEN_ON_PORT = 3000;
function toTitleCase(str) {
return str.replace(/[a-z]*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
http.createServer(function (req, res) {
var body;
body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', function() {
console.log(req.method + ' ' + req.url + ' HTTP/' + req.httpVersion);
for (prop in req.headers) {
console.log(toTitleCase(prop) + ': ' + req.headers[prop]);
}
if (body.length > 0) {
console.log('\n' + body);
}
console.log('');
res.writeHead(200);
res.end();
});
req.on('err', function(err) {
console.error(err);
});
}).listen(LISTEN_ON_PORT, function () {
console.log('Server listening on port ' + LISTEN_ON_PORT);
});
@pranaygp
Copy link

This was really helpful. I'd suggest making it an npm package.

@hcl1687
Copy link

hcl1687 commented Sep 23, 2020

It's very helpful, thank you.

@noreplydev
Copy link

Really really helpful, thanks man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment