The middleware

The express middleware is essentially a function handler. The format of express middleware is as follows:

const express=require('express');
const app=express();
app.get('/'.function(req,res,next){
    consoleThe log (middleware')}) app.listen(80,()=>{console.log(Express server is running at HTTP :127.0.0.1:80); })Copy the code

** Note: ** Middleware functions must contain the next() parameter in the parameter list, while routing functions only contain req and res

What the next() function does: The next function is the key to making multiple middleware calls in a row, indicating the transfer of the flow relationship to the next middleware or route

Implement a simple middleware

const express=require('express');

const app=express();
// Define the simplest middleware
const mv=function(req,res,next){
    console.log('This is the simplest middleware.')
    // Pass the flow relationship to the next middleware or route
    next()
}
app.listen(80.() = >{
    console. log('express server is running at http://127.0.0.1:80')})Copy the code

Globally valid middleware

The middleware that is triggered by any request made by the client after it reaches the server is called globally valid middleware. Use (middleware function) to call a globally valid middleware, example code is as follows:

// The constant mv refers to a middleware function
const mv=function(req,res,next){
    console.log('This is the simplest middleware function');
    next()
}
// Globally valid middleware
app.use(mv)
Copy the code

The role of middleware

Multiple middleware share the same REQ and RES. Based on this feature, we can agree to add custom attributes or methods for REQ and RES in upstream middleware for use by downstream middleware or routing

const express=require('express');

const app=express();
// Define the simplest middleware
const mv=function(req,res,next){
    // Get the time when the request arrives at the server
    const time=Date.now();
    // Hang a custom property for the req object to share time with all subsequent routes
    req.startTime=time;//startTime is a custom attribute
    
    // Pass the flow relationship to the next middleware or route
    next()
}
  app.use(mv);

  app.get('/'.(req,res) = >{
      res.send('get>>>>>>>>>>>>>>>>'+req.startTime)

  })
  app.post('/'.(req,res) = >{
      res.send('post............... '+req.startTime)
  })
app.listen(80.() = >{
    console. log('express server is running at http://127.0.0.1:80')})Copy the code

Define multiple global middleware

You can use app.use() to define multiple global middleware in succession, and when client requests arrive at the server, they are invoked in the order defined by the middleware, as shown in the following example code

const express=require('express'); Const app=express() // Define middleware app.use((req,res,next)=>{console.log(' called first global middleware '); Next ()}) app. Use ((the req, res, next) = > {the console. The log (' call the second global middleware), next ()}) / / routing app. Get ('/user ', function (the req, res) { Res.send ("home page")}) app.listen(80,()=>{console.log(' Express server running at http://127.0.0.1:80')})Copy the code