Created
October 22, 2023 10:25
-
-
Save akunzai/e245cb079d90bf102b8a7bf60ef07536 to your computer and use it in GitHub Desktop.
a mini HTTP server to inspect requests
This file contains 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
// 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