Nodejs processing a GET request

Source: Internet
Author: User

The main record is the problem of getting the parameters inside the GET request.

Suppose there is such a link

urlstring= ' Hello?name=lilei&position=general '

To extract the value of the parameter name and position from this link. The URL module and the QueryString module are used here, so the two objects are prepared first.

var url = require ("url"); var querystring = Require ("querystring");

The urlstring is then converted into a URL object and the parameter string "Name=lilei&position=general" is extracted

var // Converts a string urlstring into a URL object urlstring var QueryString = Urlstring.query; // gets the argument string, at which time querystring= "Name=lilei&position=general"

Then use Querystring.parse () to convert the parameter string querystring to the object containing the key-value pair params

var params = Querystring.parse (querystring);

Then use the params["name"], params["position"] (or Params.name, params.position) to get the value of the parameter name, Posiotion. The above two steps can also be combined to write

var params = Querystring.parse (Url.parse (urlstring). query);

Here's a small example (omitting the non-critical code):

varHandle = {};varurl = require ("url");varQueryString = Require ("querystring"); handle["/hello"] =Hello;functionHello (Request, response) {/*Url.parse converts a string into a URL object, Url.parse (). Query gets the argument string inside the URL, such as "name=lilei&position=general", and then uses Querystring.par SE (query), converts "name=lilei&position=general" into an object that consists of key-value pairs, such as {name: "Lilei", Position: "General"}*/    varparams =Querystring.parse (Url.parse (request.url). query); Response.writehead ({"Content-type": "Text/html;charset=utf-8"});//add Charset=utf-8 to support ChineseResponse.Write ("Name:" + params.name + "<br/>"); Response.Write ("Position:" + params.position + "<br/>"); /*or you can use traversal as follows*/    /*For (var param in params) {Response.Write (param + ":" + Params[param] + "<br/>"); }*/Response.End ();} Exports.hello= Hello;

Nodejs processing a GET request

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.