http请求中的这个语法是什么意思?
What does this syntax in http request mean?
我需要理解这行代码是什么意思
app.get("/users/:id", function(req, res){
var data = userModel.find().where('username', req);
res.send(data);
});
我不明白的部分是“/users/:id”,特别是:id 部分。这个http请求的语法是什么意思?
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
在您上面的代码中,向 /users/42
发送 GET 请求将导致 42
存储在 req.params.id
。
本质上,:id
告诉 express 请求 URI 中 :id
在路由声明中的任何内容都应该被解释为存储在 req.params
对象上 属性 id
.
的名字
您很可能想要更类似于此的内容:
app.get("/users/:id", function(req, res){
var data = userModel.find().where('id', req.params.id);
res.send(data);
});
我需要理解这行代码是什么意思
app.get("/users/:id", function(req, res){
var data = userModel.find().where('username', req);
res.send(data);
});
我不明白的部分是“/users/:id”,特别是:id 部分。这个http请求的语法是什么意思?
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
在您上面的代码中,向 /users/42
发送 GET 请求将导致 42
存储在 req.params.id
。
本质上,:id
告诉 express 请求 URI 中 :id
在路由声明中的任何内容都应该被解释为存储在 req.params
对象上 属性 id
.
您很可能想要更类似于此的内容:
app.get("/users/:id", function(req, res){
var data = userModel.find().where('id', req.params.id);
res.send(data);
});