learning express step(六)

code: use application middleware

var express =  require('express');
var app = express();

app.use(function (req, res, next) {
    console.log('Time', Date.now());
    next()
});

app.use('/user/:id', function (req, res, next) {
    console.log('Request Type:', req.method);
    next();
},function (req, res, next) {
    console.log('Request URL', req.originalUrl);
    next();
});

app.get('/user/:id', function(req, res){
    res.send("USER");
});
app.listen(3000);

result:

C:\Users\admin\WebstormProjects\learning-express-step6>node learning-express-step6.js
Time 1560331355675
Request Type: GET
Request URL /user/id=123
Time 1560331371325
Request Type: GET
Request URL /user/id=123

web result:  pass id field

 

posted @ 2019-06-12 17:26  嵌入式实操  阅读(99)  评论(0编辑  收藏  举报