Task: We knock on the nodejs server (like this: curl 127.0.0.1:8888), which, in turn, knocks on the script. The response received from the script server returns. In my case, the script responds:

echo "Auth: 1"; 

The answer comes to me in the form of two lines:

 7 Auth 

Here is the code:

 var http = require("http"); var app = http.createServer(function(request, response) { console.log ("Success!"); console.log("Request URL: "+request.url); response.writeHead(200); var scriptRequest = http.request( { hostname: "test.master12.ru", port: 80, path: "http://test.master12.ru/index.php", method: "GET", headers: request.headers || {} }); scriptRequest.on("response", function (scriptResponse) { var headers = scriptResponse.headers; response.writeHead(200, headers); scriptResponse.setEncoding('utf8'); scriptResponse.on("data", function (data) { console.log("Remote script response: "+data); response.write(data); }); scriptResponse.on("close", function() { response.writeHead(scriptResponse.statusCode); response.end(); }); scriptResponse.on("error", function (err) { console.log("Error:"+err); response.end(); }); scriptResponse.on("end", function () { console.log("End script response"); response.writeHead(scriptResponse.statusCode); response.end(); }); }); request.on("data", function (data) { console.log("Client request:"+data); scriptRequest.write(data); }); request.on("end", function () { console.log("End client request."); scriptRequest.end(); }); }); app.listen(8888, "127.0.0.1"); 

That is, the server gives me the length of the response and the first 4 characters. In this case, the console displays everything correctly.

  • It seems to you that the server responds in chunked format. - Alexey Ten
  • And what did scriptResponse.pipe(response) not suit you? - Dmitriy Simushev pm
  • @Alexey Ten, changed response.writeHead(200); in the 6th line response.writeHead(200); on response.writeHead(200, {'Transfer-Encoding' : ''}); and it worked. Thank. - Roman Notaev
  • @Dmitriy Simushev, I’m still getting to know node.js, I know very little. I will definitely try to solve the same problem with *.pipe . Thank. - Roman Notaev

0