express(中间件)
2017-07-27 本文已影响0人
悟空你又瘦了
- 只要访问一次,就会创建一次时间,当访问了/user 执行两部操作
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log(req.url + '访问时间' + Date.now());
next()
});
app.get('/user', (req, res, next) => {
console.log('result');
next();
},(req, res) => {
console.log("result1");
res.end("result1");
});
app.listen(3000, ()=>{
console.log('runing……');
})
- 可以用变量定义中间件
const express = require('express');
const app = express();
let cb0 = (req, res, next) => {
console.log('Cb0');
next();
};
let cb1 = (req, res, next) => {
console.log('Cb1');
next();
};
let cb2 = (req, res) => {
res.send('Hello Tom');
};
app.get('/user',[cb0, cb1, cb2]);
app.listen(3000, ()=>{
console.log("running……");
})
- 访问次数和访问时间
const express = require('express');
const app = express();
let count = 0;
app.use((req, res, next) => {
console.log(req.url + '访问时间' + Date.now());
next();
});
app.use((req, res, next) => {
count ++;
console.log(count);
next()
});
app.get('/login',(req, res) => {
res.end('success');
});
app.listen(3000, ()=>{
console.log("running……");
})