Proper way to remove middleware from the Express stack?

后端 未结 7 2118
时光说笑
时光说笑 2020-12-18 18:41

Is there a canonical way to remove middleware added with app.use from the stack? It seems that it should be possible to just modify the app.stack array directl

7条回答
  •  别那么骄傲
    2020-12-18 19:20

    We can write like this.

    // route outside middleware
    
    route.get("/list", (req, res)=>{
        res.send("from listing route");  
    });
    
    //use middleware
    
    router.use(Middlewares.AuthMiddleware.isValidToken);
    
    //routes inside the middleware
    
    route.post("/create", (req, res)=>{
        res.send("from create route");  
    });
    
    route.delete("/delete", (req, res)=>{
        res.send("from delete route");  
    });
    

    So basically, write routes before injecting middleware into your route.

提交回复
热议问题