21-Node.js学习笔记-Express-请求处理-app.use方法传递函数调用语法的解释

app.use方法传递函数调用语法的解释

//引入express框架
const express  = require('express');
const bodyParser = require('body-parser');
//创建网站服务器
const app = express();

//拦截所有的请求
app.use(fn({a: 1}))
function fn(obj){
    return function(req,res,next){
        if(obj.a==2){
            console.log(req.url)
        }else{
            console.log(req.method)
        }
        next()
    }
}
app.get('/',(req,res)=>{
    //获取请求参数
    res.send('ok')
})

//监听端口
app.listen(3000);
console.log('网站服务器启动成功');

猜你喜欢

转载自www.cnblogs.com/foreverLuckyStar/p/12089502.html
今日推荐