This time has been looking at the express framework of the API, recently saw router, the following is what I think need to be aware of the place:
Router module has a Param method, just start to look a little vague, the official website is probably described as follows:
1 |
Map logic to route parameters. |
It probably means that the mapping logic of the route parameter
This may be 1:30 and will not understand the function, especially do not know the order of Get and Param execution
Then look at the source of the introduction:
123 |
Map the given param placeholder `name`(s) to the given callback. Parameter mapping is used to provide pre-conditions to routes which use normalized placeholders |
This is much clearer, translation comes to say:
Make a mapping between the given parameter and the callback function as a precondition for routing using the normalized placeholder.
A specific code is given below:
1234567891011121314151617 |
var
express = require(
‘express‘
);
var
app = express();
var
router = express.Router();
router.count = 0;
router.get(
‘/users/:user‘
,
function
(req, res, next) {
router.count ++;
console.log(router.count);
});
router.param(
‘user‘
,
function
(req, res, next, id) {
router.count ++;
res.send({count: router.count});
next();
});
app.use(router);
app.listen(3000);
|
Command Line Input
Node Xxx.js
Browser access
Http://localhost:3000/users/bsn
This time the command line outputs 2, and the browser outputs
{Count:1}
Therefore, the Param is executed before the get
node. JS--There is a Param method in the router module