What does a colon mean on a directory in node js?

2020-04-10 04:38发布

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?

This is the example I saw:

app.post('/contest/vacation-photo/:year/:month', function(req, res){

2条回答
Juvenile、少年°
2楼-- · 2020-04-10 04:53

If you're talking about the :year or :month, there are URL parameters. You can get back with 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.

查看更多
够拽才男人
3楼-- · 2020-04-10 05:01

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

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.

查看更多
登录 后发表回答