I am trying to create an echo server with Node.js so it should read body of incoming requests and send that same body as the response. You can see below my code. Basically when I run it and on another terminal, I type curl --location --request POST 'localhost:3000' --header 'Content-Type: text/plain' --data-raw 'Test message' to test it, it prints the body inside the curl terminal and server terminal, but not on localhost.
const http = require('http');
http.createServer((request, response) => {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
response.writeHead(200, { 'Content-Type': 'text/plain' });
body = Buffer.concat(body).toString();
response.write(JSON.stringify(body));
console.log(JSON.stringify(body));
response.end();
});
}).listen(3000, 'localhost');
curl doesn't print the request body, it prints the response body the server is returning to you. In other words - this echo server works as planned.