http请求中的这种语法是什么意思?

时间:2015-12-23 21:02:32

标签: node.js http-get

我需要了解这行代码意味着什么

app.get("/users/:id", function(req, res){
         var data = userModel.find().where('username', req);
         res.send(data);
     });

我不理解的部分是" / users /:id",特别是:id部分。 http请求的这种语法是什么意思?

2 个答案:

答案 0 :(得分:1)

Express uses the : to denote a variable in a route.
For example /user/42 will render a request for user id - 42
            /user/7  will render a request for user id - 7
but can be represented as a consistent form /users/:id
where id is the variable, : represents that whatever is after it is a 
variable, like here we have :id - id being the variable.

for reference check this out: http://expressjs.com/en/api.html

答案 1 :(得分:1)

在上面的代码中,向/users/42发送GET请求会导致42存储在req.params.id

基本上,:id告诉表明,路由声明中:id所在的请求URI中的任何内容都应解释为存储在req.params对象上,其属性名称为{{1 }}

你很可能想要更类似的东西:

id