Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating http echo server in node.js

What I need to do is simple node.js http echo server. However, servers that I found on node website and on some other places either don't work how I would like or I don't understand how they work.

I used this simple echo server and on my client side (written in java) I don't get the results I would want to.

var http = require('http');

http.createServer(function(request,response){

 response.writeHead(200);
 request.pipe(response);

}).listen(8080);

I would like my server to make a response consisting of response header and response body where I would have the whole http request that my client sent and got back. Client side is measuring time from sending a request and getting a whole response back. Client is also writing a response body to a file.

When I try using something like

 response.write(request.url);

I get the request.url in the response body and it works but I would like to have the whole http request inside.

Any help would be appreciated.

like image 225
silentcoder Avatar asked Mar 17 '26 02:03

silentcoder


1 Answers

use:

response.write(JSON.stringify(request.headers))
like image 190
neo Avatar answered Mar 18 '26 15:03

neo