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){
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", };
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With