Skip to content

Instantly share code, notes, and snippets.

@akunzai
Last active September 18, 2024 19: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

Run this script from GitHub Gist directly

npx https://gist.github.com/akunzai/e245cb079d90bf102b8a7bf60ef07536
{"name": "inspect-requests", "version": "1.0.0", "bin": "./server.js"}
#!/usr/bin/env node
// 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