Software Engineering :: Programming :: Languages :: JavaScript :: Runtimes :: Node.js :: HTTP :: Server :: About :: Anatomy of an HTTP Transaction
⪼ Made with 💜 by Polyglot.
- Software Engineering :: Programming :: Languages :: JavaScript :: Library :: wroute
- Software Engineering :: Programming :: Languages :: JavaScript :: Runtimes :: Node.js :: HTTP :: Server :: IncomingMessage
const http = require('node:http');
http
.createServer((request, response) => {
const { headers, method, url } = request;
let body = [];
request
.on('error', err => {
console.error(err);
})
.on('data', chunk => {
body.push(chunk);
})
.on('end', () => {
body = Buffer.concat(body).toString();
// BEGINNING OF NEW STUFF
response.on('error', err => {
console.error(err);
});
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
// Note: the 2 lines above could be replaced with this next one:
// response.writeHead(200, {'Content-Type': 'application/json'})
const responseBody = { headers, method, url, body };
response.write(JSON.stringify(responseBody));
response.end();
// Note: the 2 lines above could be replaced with this next one:
// response.end(JSON.stringify(responseBody))
// END OF NEW STUFF
});
})
.listen(8080);