Skip to content

Instantly share code, notes, and snippets.

@bszwej
Last active April 26, 2024 05:22
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save bszwej/62c327d773051816ed4949fd40c82c74 to your computer and use it in GitHub Desktop.
Save bszwej/62c327d773051816ed4949fd40c82c74 to your computer and use it in GitHub Desktop.
Pure Node.js echo server, that logs all incoming http requests (method, path, headers, body).
const http = require('http');
const server = http.createServer();
server.on('request', (request, response) => {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
console.log(`==== ${request.method} ${request.url}`);
console.log('> Headers');
console.log(request.headers);
console.log('> Body');
console.log(body);
response.end();
});
}).listen(8083);
@Aschen
Copy link

Aschen commented Oct 15, 2023

The oneliner version, just copy past in your terminal:

node -e "const h = require('http');const s = h.createServer();const l = console.log;s.on('request', (rq, rs) => {let b = [];rq.on('data', (c) => {b.push(c);}).on('end', () => {b = Buffer.concat(b).toString();l('==== '+rq.method+' '+rq.url);l('> Headers');l(rq.headers);l('> Body');l(b);rs.end();});}).listen(8000);"

@meenabassem
Copy link

The oneliner version, just copy past in your terminal:

node -e "const h = require('http');const s = h.createServer();const l = console.log;s.on('request', (rq, rs) => {let b = [];rq.on('data', (c) => {b.push(c);}).on('end', () => {b = Buffer.concat(b).toString();l('==== '+rq.method+' '+rq.url);l('> Headers');l(rq.headers);l('> Body');l(b);rs.end();});}).listen(8000);"

Many thanks for this bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment