URL module, path module, querystring module

Source: Internet
Author: User

a The URL is complete, including the QueryString part (that is , the GET request query string part),thehash part

Http://127.0.0.1:3000/b.html?id=123#123

At this point the Req.url is:

In other words, querystring belong to Req.url, but hash does not belong.

Now we want the part of the file name

The built-in modules are available in node: URL, path, querystring they can all serve URL recognition

var http = require ("http"); var fs = require ("FS"); var url = require ("url"); Http.createserver ((req,res)+=    {// to Object     var urljson = url.parse (req.url);    Console.log (Urljson);    Res.end ("");}). Listen ("127.0.0.1");

The URL is:

Http://127.0.0.1:3000/haha/1.html?id=123&name= Xiao Ming &sex= man #abc

The objects that are output at this time are:

If you add url.parse (Req.url,true), the QueryString section will automatically become an object, so that we can deposit into the database or something.

Two additional modules: path, querystring they are all service URLs.

varHTTP = require ("http");varFS = require ("FS");varurl = require ("url");varPath = require ("path");varQueryString = Require ("querystring"); Http.createserver ((Req,res)={    //Convert the URL string into a JSON object.     varUrljson =Url.parse (Req.url); //Get file path    varpathname =Urljson.pathname; //Get Extended name    varExtname =path.extname (pathname); //Get query string    varQS =Urljson.query; //to query the object, and Url.parse plus true very similar    varQsjson =Querystring.parse (QS);    Console.log (pathname);    Console.log (Extname);    Console.log (Qsjson); Res.end ("");}). Listen (3000, "127.0.0.1");

URL module, path module, querystring 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.