Current location - Quotes Website - Personality signature - Second-ranked middleware, routing, router, https
Second-ranked middleware, routing, router, https
This chapter mainly studies the two biggest features of Express in detail: middleware and routing.

Middleware exists in many frameworks. Express middleware divides a large request processor into several small parts for processing. Middleware can logically control the request and response objects, thus returning the expected results.

Express uses the method of app.use () to add middleware to the middleware stack to form a function array, and uses FIFO (first in first out) to process data in turn.

Using the NodeJS framework of the server, the process from the request initiated by the client to the response received is roughly as follows:

When the middleware stack finishes processing, it either generates an error and exits, or calls the res.end () method, or the res.send () and res.sendfile () methods (these two methods automatically call the res.end () method).

The most common forms of middleware are:

The last middleware' next' can be omitted, for example, it does not match the route, and return to 404:

The following example is used to write two middleware, one for logging and the other for sending files:

Of course, morgan middleware provided by a third party can also be used to replace the above log middleware and express.static () that comes with express instead of sending files. The above example can be rewritten as:

Its signature is:

If you enter the error mode, other normal middleware will not handle it, so the general error handling is put at the end, just like' catch' in the Promise.

Add a parameter to next (), usually an error object, to enter error mode:

Example:

In the previous chapter, we briefly introduced routing. Simply put, routing is: URL+HTTP request action ('get',' post',' put',' delete ')...)- > corresponding response handler.

Now we will introduce routing in more detail, such as static file routing problem and the use of routers.

In the previous chapter, we discussed three path matching methods: string, string template and regular expression. Here are a few examples:

Let's talk about the property req.params:

When using named routing parameters, this attribute is an object, such as "/user/:name", and "name" is an attribute of the "req.params" object.

When using regular expressions to define routing paths, you can get the captured groups through' req.params[n]':

Query in search engines, often encounter this form of routing'/search? Q=javascript20%tutorials', which can be handled in the following ways:

Use req.query to get the query string:

Routers are independent instances of middleware and routing. You can split a large application into many small applications. Every express application has a built-in application router.

Instantiate the router with express. Router ()

Main application:

Router:

There are several methods for routers, as follows:

Optional parameters are used to define the behavior of the router. There are three optional parameters:

This method is similar to a router. Method (), except that it matches all HTTP request operations ('GET',' POST') ...).

Very useful for global logic under the specified path, such as:

Returns a single routing instance that can be called in a chain to avoid duplication.

The following situations:

1. For example, we visited www.example.com/dog.jpg'', and now we want to go through www.example.com/gallery/dog.jpg''.. We can do this in the following ways.

2. Multiple static file paths. Sometimes files may be in different folders. We can call the' express.static ()' method several times to add the location of the static file.

This situation may exist in the following ways:

3. The fourth situation above can be solved in the following ways.

Now users can get both' /public' and' /uploads' resources, such as' /public/cat.png' and' /uploads/dog.png'

HTTPS adds a layer of security protocol (TSL (superior to ssl) or SSL).

Popular explanation: each device has a public key (google calls it a certificate such as CAs) and a private key. It sends information, encrypts it with the private key, and the other party receives the information, identifies it with the public key, and then decrypts it with its own private key.

In order to use HTTPS, you need to perform the following steps:

This chapter mainly introduces the working principle and definition form of middleware in detail; Some methods when using routing, how to use routers to divide apps into small apps, and some methods commonly used in router API. Finally, we talked about how to use the https module and how to handle the https protocol.

2065438+March 20, 2007 19:40:57