There's more...

Route paths can be strings or regular expressions. Route paths are internally turned into regular expressions using the path-to-regexp NPM package https://www.npmjs.com/package/path-to-regexp.

path-to-regexp, in a way, helps you write path regular expressions in a more human-readable way. For example, consider the following code:

app.get(/([a-z]+)-([0-9]+)$/, (request, response, nextHandler) => { 
    response.send(request.params) 
}) 
// Output: {"0":"abc","1":"12345"} for path /abc-12345 

This could be written as follows:

app.get('/:0-:1', (request, response, nextHandler) => { 
    response.send(request.params) 
}) 
// Outputs: {"0":"abc","1":"12345"} for /abc-12345 

Or better:

app.get('/:id-:tag', (request, response, nextHandler) => { 
    response.send(request.params) 
}) 
// Outputs: {"id":"abc","tag":"12345"} for /abc-12345 

Take a look at this expression: /([a-z]+)-([0-9]+)$/. The parentheses in the regular expression are called capturing parentheses; and when they find a match, they remember it. In the preceding example, for abc-12345, two strings are remembered, {"0":"abc","1":"12345"}. This is the way that ExpressJS finds a match, remembers its value, and associates it to a key:

app.get('/:userId/:action-:where', (request, response, nextHandler) => { 
    response.send(request.params) 
}) 
// Route path: /123/edit-profile 
// Outputs: {"userId":"123","action":"edit","where":"profile"} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.37