简体   繁体   中英

Node dynamic routing when route is undefined

I am new with node and am trying to figure out dynamic routing. I have routes already set up such as

app.route('/users')
    .get(dbController.collect)
    .post(dbController.insert);

  app.route('/users/:userId')
    .get(dbController.read)
    .put(dbController.update)
    .delete(dbController.delete);

I want to be able to do something similar for routes that I have not defined in my code while still letting the routes I have defined work as usual. For example if someone was to send a get request to https://example.com/books/12 it would dynamically run the read function as it does for users.

Express routing allows you to do catch-all routes:

app.all('*', (req, res) => {
  console.log('Route is unknown')
  // Do something here...
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM