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