Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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){
like image 376
Vandervals Avatar asked Aug 31 '15 14:08

Vandervals


People also ask

What does colon mean in JS?

The colon symbol ( : ) is generally used by JavaScript as a delimiter between key/value pair in an object data type. For example, you may initialize an object named car with key values like brand and color as follows: let car = { brand: "Toyota", color: "red", };

How do I read a directory in node JS?

The fs. readdir() method is used to asynchronously read the contents of a given directory. The callback of this method returns an array of all the file names in the directory. The options argument can be used to change the format in which the files are returned from the method.

Should I use semicolon NodeJS?

Stop Using Semicolons with Node. js. Semicolons are actually optional, because ECMAScript (the standard for Node. js and browser JavaScript implementations) has an automatic semicolon-insertion feature (ASI).

What is writeFileSync in node JS?

writeFileSync() is a synchronous method, and synchronous code blocks the execution of program. Hence, it is preferred and good practice to use asynchronous methods in Node. js.


1 Answers

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.

like image 52
Black0ut Avatar answered Sep 24 '22 21:09

Black0ut