Contents of this series

1. GlobalI went to see

2. Routes are matchedI went to see

3. Error handlingI went to see

4. View templateI went to see

5. Design Idea (This paper)


1. Everything is middleware

In the Express world, everything can be used as middleware, and all middleware is stacked on top of each other like a pie. Each middleware communicates data (if necessary) through shared REQ and RES objects, which are usually not communicated, and each plays its own game. App can only be app, right? App can only use router?

The pattern of small

1.1 the app use app

var app = express();
const app2 = express();
app2.use('/'.function (req, res, next) {
 res.end('app2');
})
app.use('/app2', app2);
Copy the code

1.2 the router use the router

var express = require('express');
var router = express.Router();
var router2 = express.Router();
router2.use('/'.function (req, res, next) {
 res.end('router2')})/* GET home page. */
router.get('/router2', router2);
Copy the code

1.3 the router use app

var express = require('express');
var router = express.Router();
var app = express();
app.use('/'.function (req, res, next) {
 res.end('app');
})
router.get('/', app);
Copy the code

Of course, it is impossible to write such nesting before ten years old. All these just show that Express is only a small server system, which can be combined into a large server application, written as an APP according to each part, and then used as a global app


2. Sequential execution middleware

As is known to all, Express middleware is one by one, not a call next, next again how urgency can execute, and the order of execution in strict accordance with the order to use, is the location of the code will affect the order of execution, which requires middleware, only responsible for that part of yourself, don’t talk to other middleware coupling, It’s the only way to ignore the order, just like the commutative law.


Problem 3.

Sequential execution is simple, but many problems can arise

3.1 cut hu

Either middleware could call res.end directly to terminate the process.

/ / truncation
app.use(The '*'.(req, res) = > {
  res.end('cut hu')})//set the routers
app.use('/', routers.rootRouter);
app.use('/user', routers.userRouter);
Copy the code

3.2 The destruction of the universe is not easy to understand, the last middleware appeared an infinite cycle

app.use(The '*'.function(req,res,next){
    // Have a moment to yourself
    while(true){
        drink();
        eat();
        play();
    }
})
app.use('/'.function(req,res,next){
    req.help = true;
    next();
})
Copy the code

3.3 Destroying the Earth

app.use(The '*'.function (req, res, next) {
  delete req.app.res
  delete req.app
  Object.freeze(req);
  next();
})
app.use('/', routers.rootRouter);
app.use('/user', routers.userRouter);
Copy the code

4. Best route writing (personal opinion)

Since routers can use routers, nested routers can be nested according to the classification of routes when writing nested routines, and each router is equipped with a complete middleware mechanism, so it can be nested according to this

The end of the

Express source code series finished, at first because the contact of the first node end server framework is Express, and then see the number of source lines is not much, just decided to study, before and after a week of time-consuming, shallow talent, write also relatively shallow, hope more inclusive! 🙌