简体   繁体   中英

Express.js - Allow infinite subroutes, but at least one

I want to allow any subroute after /example/... .

Example of valid URLs:

  • /example/one
  • /example/one/two
  • /example/one/two/three

But doing this:

server.get('/example/*', (req, res) => {
   // ...
})

Is also allowing /example without any subroute. And I want to avoid this. How can I do it? Is there any way without using regex?

Thank you so much!

You could simply define two routes - one for /example and another for all /example -subroutes, you need to make sure the order is correct though:

app.get('/example', (req, res) => {     
    // render default page
});

app.get('/example/*', (req, res) => {
   // do other stuff
});

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