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:UsersadminWebstormProjectslearning-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