Last active
May 25, 2024 05:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
It's very helpful, thank you.
Really really helpful, thanks man.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was really helpful. I'd suggest making it an npm package.