Skip to content

Instantly share code, notes, and snippets.

@akunzai
Created October 22, 2023 10:25
Show Gist options
  • Save akunzai/e245cb079d90bf102b8a7bf60ef07536 to your computer and use it in GitHub Desktop.
Save akunzai/e245cb079d90bf102b8a7bf60ef07536 to your computer and use it in GitHub Desktop.
a mini HTTP server to inspect requests
// see alternative tool at https://www.npmjs.com/package/reqon
const http = require('http');
const port = process.argv[2] || 3000;
const server = http.createServer((req, res) => {
console.log('--- ', new Date().toISOString());
console.log(req.method, req.url);
for (let header in req.headers) {
console.log(`${header}: ${req.headers[header]}`);
}
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
if (body.length > 0) {
console.log();
console.log(body);
}
});
res.statusCode = 202;
res.end();
});
server.listen(port);
console.log(`Server running on port ${port}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment