"Node. js" Get/post request, Web module

Source: Internet
Author: User

Get the GET Request content

This function is provided by the parse function in the URL module in node. js.

var http = require (' http '); var url = require (' URL '); var util = require (' util '); Http.createserver (function(req, res) {    Res.writehead ( , {' Content-type ': ' Text/plain; Charset=utf-8 '});     true )));}). Listen (3000);

Gets the parameters of the URL

 var  http = require (' http '  function   (req, res) {Res.writehead (      $, {' Content-type ': ' Text/plain ' });  //  parse URL parameter  var  params = Url.parse (Req.url, true      site name: "+ Params.name);    Res.write ( \ n " site URL: "+ Params.url); Res.end (); }). Listen (); 

Get POST Request Content

varHTTP = require (' http ');varQueryString = Require (' querystring ')); varposthtml = ' ; Http.createserver (function(req, res) {varBODY = ""; Req.on (' Data ',function(chunk) {body+=Chunk;  }); Req.on (' End ',function () {    //parsing ParametersBODY =Querystring.parse (body); //Setting the response header information and encodingRes.writehead, {' Content-type ': ' text/html; Charset=utf8 '}); if(Body.name && Body.url) {//output the submitted dataRes.write ("Site name:" +body.name); Res.write ("<br>"); Res.write ("Site URL:" +Body.url); } Else{//Output FormRes.write (posthtml);  } res.end (); });}). Listen (3000);

Use Node to create a WEB server (server to create servers, resolve requests, read file contents, send response data (response headers, response content))

Create Server.js

varHTTP = require (' http ');varFS = require (' FS ');varurl = require (' URL '));//Creating a serverHttp.createserver (function(Request, response) {//parse the request, including the file name   varpathname =Url.parse (request.url). Pathname; //the file name of the output requestConsole.log ("Request for" + Pathname + "received."); //read the requested file contents from the file systemFs.readfile (PATHNAME.SUBSTR (1),function(err, data) {if(Err) {Console.log (err); //HTTP Status code: 404:not FOUND         //Content Type:text/plainResponse.writehead (404, {' Content-type ': ' text/html '}); }Else{                      //HTTP Status code: 200:ok         //Content Type:text/plainResponse.writehead, {' Content-type ': ' text/html '}); //Response File ContentsResponse.Write (Data.tostring ()); }      //Send response DataResponse.End ();   }); }). Listen (8081);//the console will output the following informationConsole.log (' Server running at http://127.0.0.1:8081/');

Create a index.html

<HTML><Head><title>Sample Page</title></Head><Body>Hello world!</Body></HTML>

After entering http://127.0.0.1:8081/index.html

Use Node to create a WEB client (requests to the server, requested options, constant updating of data, data reception complete)

varHTTP = require (' http ');//options for the requestvarOptions ={host:' localhost ', Port:' 8081 ', Path:'/index.htm '  };//callback function for handling responsesvarcallback =function(response) {//keep updating your data   varBODY = ' '; Response.on (' Data ',function(data) {body+=data;      }); Response.on (' End ',function() {      //Data Reception CompleteConsole.log (body); });}//sending requests to the servervarreq =http.request (options, callback); Req.end ();

Get results after running

"Node. js" Get/post request, Web module

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.