1. What is Node?
The following is taken from MDN
Node (formally node.js) is an open source, cross-platform runtime environment that allows developers to create a variety of server-side tools and applications using JavaScript. This runtime is primarily used outside of a browser context (that is, it can run directly on a computer or server operating system). Accordingly, the environment omits some browser-specific JavaScript apis while adding support for more traditional OS apis such as HTTP libraries and file system libraries.
2. What is Express
MDN Express is the most popular Node framework and is the underlying library for many other popular Node frameworks. It provides the following mechanisms:
- Write handlers for requests (routes) that use different HTTP verbs in different URL paths.
- A “View” rendering engine is integrated to generate responses by inserting data into templates.
- Set common Web application Settings, such as the port to connect to, and the location to render the response template.
- Add additional request processing “middleware” anywhere in the request processing pipeline.
You can see that Express is a Node framework. Where additional processing is added at any location of the request. Middleware is what we’re talking about.
3. What is middleware
What is middleware? Middleware sounds a little spooky. The essence of middleware is function. Middleware functions that can be interpreted as inserting various functions during request processing. Every intermediate function can be very simple. Middleware services with many functions can be complex. Lots of features. So here’s an intermediate function. You can see that the function takes three arguments. Request object (REQ), response object (RES), and the next middleware function in the application’s request/response loop. There is also a key next() function. If the current middleware function does not end the request/response loop, it must call next() to pass control to the next middleware function. Otherwise, the request remains pending.
const express = require('express'); const app = express(); // Example middleware function const a_MIDDLEware_function = (req, res, next) => {//... Do something next(); // Call next() and Express will call the next middleware function in the processing chain. }; App.use (a_middleware_function); // Add the function app.use(a_middleware_function) for all routes and verbs with use();Copy the code
Let’s look at what will be printed if the Node service is accessed.
Const express = the require () 'express' const app = express () const port = 3000 / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / basically see this part const a_middleware_function = (req, res, next) => { console.log("befor a") next(); // Call next() and Express will call the next middleware function in the processing chain. console.log("after a") }; // Example middleware function const b_middleware_function = (req, res, next) => {console.log("befor b") next(); // Call next() and Express will call the next middleware function in the processing chain. console.log("after b") }; App.use (a_middleware_function); // Add the function app.use(a_middleware_function) for all routes and verbs with use(); app.use(b_middleware_function); ///////////////////////////////// app.get('/', (req, res) => { res.send('Hello World! ') }) app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })Copy the code
The answer is the execution order of the middleware.
4. Set up middleware projects
There are many ways to do this. Here is a method of Express Chinese. Here is the documentation. Follow the documentation. www.expressjs.com.cn/starter/gen…
After installation, the structure is as follows.
NPN run start Starts the projectCopy the code
Visit http://localhost:3000/ successfully
Let’s go back and look at the app.js file and see that there are two routes registered so LET me try users first
He’s got the route in chunks here. To access users, go to the users file under Routes
There is only one path in users. If we write another /XXX. Then its actual path is the same as users/XXX
Visit http://localhost:3000/users, no problem, according to the code to write back. /users is an interface. We’re actually just making a GET request.
And similarly, the same thing that we just did in the direct access to the path is to return the page template that we talked about earlier.
We’ll add the middleware code we wrote to app.js.
Restart at http://localhost:3000/
As you can see, whenever a request comes to our service. It goes through the middleware function.
Then we can refine our middleware as needed. It’s essentially a function. We just write middleware functions for whatever functionality we need.
5. Reference materials
1. The MDN Express/introduction to the Node
2. Express Chinese website