node. js-------using the routing module

Source: Internet
Author: User

The information required for routing, including URL and get or post parameters. The route executes the corresponding JS handler based on these parameters, so the URL and the Get or post parameters need to be extracted in the HTTP request. These request parameters are in the requests object, which is the first parameter of the ONrequest () callback function. This information needs to be extracted and requires node. JS modules, URLs, and QueryString modules.

                Url.parse (string). Query

|

     Url.parse (String). Pathname |

| |

http://localhost:8888/start? foo=Bar&hello= World

QueryString (String) ["foo"]

QueryString (String) ["Hello"]

Of course, you can use the QueryString module to interpret the parameters in the POST request body.

It is possible to map to different handlers through different URL paths for the request, and routing is doing this work.

For example, requests from:/start and/upload can be handled using a different program.

Here is an example:

---index.js

---server.js

---route.js

Write a route that route.js

Function route (pathname) {Console.log ("about-route a request for"  +  pathname);} Exports.route = route;

Write the page that handles the request, Server.js

var http = require ("http"), var url = require (' URL '), function start (route) {function onrequest (request, response) {var pathname = Url.parse (Request.url). Pathname;console.log ("Request for" + pathname + "received"); Route (pathname);// The different paths can be processed here//if (pathname = = "...") response.writehead different information such as Response.writehead ("Content-type": "text/ Plain "}"); Response.Write ("Hello World"); Response.End ();} Http.createserver (onrequest). Listen, Console.log ("Server has started."); Exports.start = start;

Writing startup files, Index.js

var server = require ("./server"), var router = require ("./router"); Server.start (Router.route);

Start the application on the client, the server starts, and starts listening on port 3000:

Node Index.js

Enter a request URL on the browser side:

Http://127.0.0.1:3000/

See the corresponding client output:

Browser display:

node. js-------using the routing 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.