I'm reading a book about nodejs/express and I'm trying to reproduce the examples. I've never seen a colon on a directory name, but I've seen it a couple of times in this book. Could you tell me what it means?
我正在读一本关于nodejs / express的书,我正在尝试重现这些例子。我从未在目录名称上看到冒号,但我在本书中已经看过几次。你能告诉我这是什么意思吗?
This is the example I saw:
这是我看到的例子:
app.post('/contest/vacation-photo/:year/:month', function(req, res){
2 个解决方案
#1
2
As SLaks stated, it's a URL pattern, the colon means that you want to receive the URL segments as parameter, here is an example
正如SLaks所说,它是一个URL模式,冒号意味着你想要接收URL段作为参数,这里是一个例子
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
in this example, if you will send a get request to the URL www.server.com/user/mike, the request.params.id will be set to mike.
在此示例中,如果您要向URL www.server.com/user/mike发送get请求,则request.params.id将设置为mike。
#2
0
If you're talking about the :year
or :month
, there are URL parameters. You can get back with req.params.
如果你在谈论:year或:month,则有URL参数。你可以用req.params回来。
For exemple to get back this two arguments you can do something like :
例如,要回到这两个参数,您可以执行以下操作:
app.post('/contest/vacation-photo/:year/:month', function(req, res){
// Get the year url parameter :
var year = req.params.year;
}
Hope it help.
希望它有所帮助。
#1
2
As SLaks stated, it's a URL pattern, the colon means that you want to receive the URL segments as parameter, here is an example
正如SLaks所说,它是一个URL模式,冒号意味着你想要接收URL段作为参数,这里是一个例子
app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});
in this example, if you will send a get request to the URL www.server.com/user/mike, the request.params.id will be set to mike.
在此示例中,如果您要向URL www.server.com/user/mike发送get请求,则request.params.id将设置为mike。
#2
0
If you're talking about the :year
or :month
, there are URL parameters. You can get back with req.params.
如果你在谈论:year或:month,则有URL参数。你可以用req.params回来。
For exemple to get back this two arguments you can do something like :
例如,要回到这两个参数,您可以执行以下操作:
app.post('/contest/vacation-photo/:year/:month', function(req, res){
// Get the year url parameter :
var year = req.params.year;
}
Hope it help.
希望它有所帮助。