node. JS--There is a Param method in the router module

Source: Internet
Author: User

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 routeswhich 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 varexpress = require(‘express‘);varapp = express();varrouter = 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.