A basic HTTP client in node sends data to the local HTTP server

Source: Internet
Author: User

The previous article mentioned that node can easily request data to other.

This article is about interacting with the data on the local server.

HTTP server code, s.js

1 varHttp=require ("http");2 varServer=http.createserver (function(req,res) {3     if(req.url!== "/favicon.ico"){4Req.on ("Data",function(data) {5Console.log ("Server received data:" +data);6 res.end ();7         })8     }9 });TenServer.listen (1337, "127.0.0.1",function(){ OneConsole.log ("Start listening port" +server.address (). port+ "..."); A});

HTTP client code, C.js:

  1  var  http=require ("http" );  var  options= {  3  hostname:" localhost ",   4  port:1337,   5  path: "/"   6  method:" POST "
     
       7 
      };   var  req= http.request (options);   req.write (" Hello " 10  Req.end ("Goodbye."); 

Run the server-side code first, run the client code, and the result is:

Since the server can accept the client's code, it is logical that the data can be sent to the client.

Modify the above code, S.js:

1 varHttp=require ("http");2 varServer=http.createserver (function(req,res) {3     if(req.url!== "/favicon.ico"){4Req.on ("Data",function(data) {5Console.log ("Server received data:" +data);6Res.write ("Hello from the server side!!"));7Res.write ("Good-bye from the server side!!!"));8 res.end ();9         });Ten     } One }); AServer.listen (1337, "127.0.0.1",function(){ -Console.log ("Start listening port" +server.address (). port+ "..."); -});


C.js Code:

1 varHttp=require ("http");2 varoptions={3Hostname: "localhost",4port:1337,5Path: "/",6Method: "POST"7 };8 varReq=http.request (Options,function(res) {9Res.on ("Data",function(chunk) {TenConsole.log ("The data received by the client:" +chunk); One     }); A }); -Req.write ("Hello"); -Req.end ("Goodbye.");

To run the code:

S.js:

C.js:

http. The Addtrailers method of the Serverresponse object appends a header message to the end of the server-side response data, and after the client receives the appended data, it can pass the parameter value object of the callback function's parameter in the callback function, that is, an HTTP. Gets information about the trailers property of the Incomingmessage object.

Continue to modify the above code:

S.js

1 varHttp=require ("http");2 varServer=http.createserver (function(req,res) {3     if(req.url!== "/favicon.ico"){4Req.on ("Data",function(data) {5Console.log ("Server received data:" +data);6Res.write ("Hello from the server side!!"));7Res.write ("Good-bye from the server side!!!"));8             //res.end ();9         });TenReq.on ("End",function(){ OneRes.addtrailers ({"CONTENT-MD5": "7895bf4b8828b55ceaf47747b"}); A res.end (); -         }); -     } the }); -Server.listen (1337, "127.0.0.1",function(){ -Console.log ("Start listening port" +server.address (). port+ "..."); -});

C.js

1 varHttp=require ("http");2 varoptions={3Hostname: "localhost",4port:1337,5Path: "/",6Method: "POST"7 };8 varReq=http.request (Options,function(res) {9Res.on ("Data",function(chunk) {TenConsole.log ("The data received by the client:" +chunk); One     }); ARes.on ("End",function(){ -Console.log ("Trailer header information:%j", res.trailers); -     }); the }); -Req.write ("Hello"); -Req.end ("Goodbye.");

To run the code:

S.js

C.js

Do not know why the client's information will be repeated output two times.

If any of the great gods know, please advise.

Bye, sleep.

A basic HTTP client in node sends data to the local HTTP server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.