Nodejs Note 2 -- Request route

Source: Internet
Author: User

For different URL requests, the server should have different responses. We need to provide the request URL and other required GET and POST parameters for the route. Then the route needs to execute the corresponding code based on the data. All the data we need will be included in the request object, which is passed as the first parameter of the onRequest () callback function. To parse the data, you need to call additional modules, namely the url and querystring modules. URL: This module has utilities for URL resolution and parsing. call require ('url') to use it. parsed URL objects have some or all of the following fields, depending on whether or not they exist in the URL string. any parts that are not in the URL string will not be in the parsed object. examples are shown for the URL 'HTTP: // user: pass@host.com: 8080/p/a/t/h? Query = string # hash 'href: The full URL that was originally parsed. both the protocol and host are lowercased. example: 'http: // user: pass@host.com: 8080/p/a/t/h? Query = string # hash 'Protocol: The request protocol, lowercased. example: 'http: 'host: The full lowercased host portion of the URL, including port information. example: 'host. com: 8080 'auth: The authentication information portion of a URL. example: 'user: pass' hostname: Just the lowercased hostname portion of the host. example: 'host. com 'port: The port number portion of the host. example: '200' pathnam E: The path section of the URL, that comes after the host and before the query, including the initial slash if present. example: '/p/a/t/H' search: The 'query string' portion of the URL, including the leading question mark. example :'? Query = string 'path: Concatenation of pathname and search. Example: '/p/a/t/h? Query = string 'query: Either the 'params' portion of the query string, or a querystring-parsed object. example: 'query = string' or {'query': 'string'} hash: The 'fragment 'portion of the URL including the pound-sign.Example: '# hash' we will use the dependency injection method to add routing modules loose. A function as a routing target is called a request processing program. to implement a request processing function, you must create a module called requestHandlers. Of course, you can also name it another module. Add a placeholder function for each request processing program, and then export these functions as a module, so that the request processing program and the routing module can be connected, make the routes traceable. Specifically, a series of request handlers are passed through an object, and the object needs to be injected into the route () function in loosely coupled mode. We can get the request processing function from the passed object in the same way as getting elements from the associated array, so there is a concise and smooth form such as handle [pathname] ();. Var handle = {} handle ["/"] = requestHandlers. start; handle ["/start"] = requestHandlers. start; handle ["/upload"] = requestHandlers. upload;

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.