Nodejs's request routing overview

  • 2020-03-30 03:30:17
  • OfStack

Generally, the server should respond differently to different URL requests. We provide the URL of the request and other required GET and POST parameters for the route, and the route then executes the code based on this data. All the data we need is contained in the request object, which is passed as the first argument to the onRequest() callback function. To parse this data, you need to invoke additional modules, namely the url and querystring modules.
 
URL: This
  Module has utilities for URL resolution and parsing. Call require(' URL ') to
  The use of it.
 
Parsed URL objects have some or all of the following fields, Depending on been or not they exist in the URL string. Any parts that are not in the URL string will not be in the parsed object. The Examples are to the 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 The 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, o port information.
Example: 'host.com: 8080'
 
Auth: The authentication information portion of a URL.
Example: 'user: pass'
 
The hostname: Just the lowercased hostname portion of the host.
Example: 'host.com'
 
: The port number on The port of The host.
Example: '8080'
 
Pathname: 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: 'the & # 63; The query string of = '
 
Path: Concatenation of pathname and search.
Example: '/ p/a/t/h & # 63; The query string of = '
 
Query: Either the 'params' portion of the querystring, 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 dependency injection to add the routing module more loosely. The function that serves as the routing target is called the request handler, and the request handler is implemented by creating a module called requestHandlers, or whatever. And for each request handler, add a placeholder function, and then export these functions as the module's methods, so that the request handler and the routing module can be connected to give way to the way.
 
Specifically, you need to pass a series of request handlers through an object and inject this object into the route() function in a loosely-coupled manner.

We can get the request handler from the passed object in the same way that we get the elements from the associative array, so we have a neat, smooth shape like handle[pathname](); Of. The code is as follows:


var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

Related articles: