0

I want to create a web application structure like the following:

rootUrl/shopRecords/shop1
rootUrl/shopRecords/shop2
rootUrl/shopRecords/shop3...

where there can be any limit of shops, but all of the shop pages will have the same page layout (same HTML/Pug page, just populated with different data from a database). These URLs will be accessed from a list of buttons/links on a home page.

Does Express.js have a wildcard character so that I can specify behavior for all pages with link format rootUrl/shopRecords/* to be the same?

C J
  • 567
  • 2
  • 4
  • 11

1 Answers1

2

This is explained in the routing section of the ExpressJS documentation.

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.

Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

To define routes with route parameters, simply specify the route parameters in the path of the route as shown below.

app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335