node. JS Routing
We want to provide the requested URL and other required get and post parameters for the route, and then the route needs to execute the corresponding code based on that data.
Therefore, we need to look at the HTTP request and extract the requested URL and the Get/post parameter from it. Whether this feature should belong to a routing or server (or even as a function of a module itself) is really worth exploring, but here it is tentatively the function of our HTTP server.
All the data we need will be included in the request object, which is passed as the first parameter of the ONrequest () callback function. But in order to parse this data, we need additional node. JS modules, which are URLs and QueryString modules, respectively.
Url.Parse(String).Query|Url.Parse(String).Pathname| | | | | ------ -------------------http://localhost:8888/start?foo=bar&hello=world --- ----- | Span class= "pun" >| || Querystring. (querystring "foo" ] || Querystring. (querystring "Hello" /span>
Of course, we can also use the QueryString module to parse the parameters in the POST request body, there will be a demo later.
Well, our app can now differentiate between requests by the URL path of the request-which allows us to use the route (not yet completed) to map the request to the handler using the URL path as a baseline.
In the application we are building, this means that requests from/start and/upload can be handled using different code. We'll see how these things are integrated later.
Now we can write the route, create a file named router.js , add the following:
function Route (pathname) { Console.log ("about-route a request for" += route;
Now Server.js Let's add some logic to the onrequest () function to find the URL path to the browser request:
varHTTP = require ("http");varurl = require ("url");functionStart (route) {functiononrequest (Request, response) {varpathname =Url.parse (request.url). Pathname; Console.log ("Request for" + Pathname + "received."); Route (pathname); Response.writehead ($, {"Content-type": "Text/plain"}); Response.Write ("Hello World"); Response.End (); } http.createserver (ONrequest). Listen (8888); Console.log ("Server has started.");} Exports.start= start;
At the same time, we will extend the index.js accordingly so that the routing function can be injected into the server:
var server = require ("./server"); var router = require ("./router"); Server.start (Router.route);
Here, the functions we pass are still not doing anything.
If you start the app now (node Index.js, always remember this command line), then request a URL, you will see the application output the appropriate information, indicating that our HTTP server is already using the routing module, and will pass the requested path to the route:
Node index. JSServer has started.
The above output has removed some of the more annoying/favicon.ico request-related sections.
The browser accesses the http://127.0.0.1:8888/and the output is as follows:
Hello World
node. JS Routing