1. Routers
Routing is how you define an app's endpoints (URIs) and how to respond to requests from clients.
A route consists of a URI, an HTTP request (GET, post, and so on) and a number of handles, which are structured as follows: an instance of an object, an app.METHOD(path, [callback...], callback)
app
express
METHOD
http request method, a path
path on a server, a callback
route The function to execute when matching.
Here is a basic example of a route:
var express =require(‘express‘);var app =express();// respond with "hello world" when a GET request is made to the homepageapp.get(‘/‘,function(req, res){ res.send(‘hello world‘);});
Routing Methods
2. Middleware
Express is a minimalist web development framework that is entirely composed of routing and middleware: Essentially, an express application is invoking a variety of middleware.
The Middleware (middleware) is a function that can access the request object (requests ( req
)), the Response object (Response object ( res
)), and the Web The middleware in the application that is in the request-response loop process is generally named as next
a variable.
The middleware features include:
- Execute any code.
- Modifies the request and response objects.
- The end request-response loop.
- The next middleware in the call stack.
If the current middleware does not have an end request-response loop, the method must be called to next()
give control to the next middleware or the request will be suspended.
There are several types of middleware that Express applications can use:
- Application-level middleware
- Routing-level Middleware
- Error handling Middleware
- Built-in middleware
- Third-party middleware
Use the optional mount path to load the middleware at the application level or at the route level. In addition, you can also install a series of middleware functions at the same time to create a sub-middleware stack on a mount point.
node. js