Multiple optional route parameters in Express?

前端 未结 3 819
借酒劲吻你
借酒劲吻你 2021-01-31 07:07

I am using Express to handle a route which is in the format of /articles/:year/:month/:day, where year, month and day are optional.

  • If none of the thr
3条回答
  •  庸人自扰
    2021-01-31 07:48

    The expressjs's guide to routing mentions:

    Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Tester is a handy tool for testing basic Express routes, although it does not support pattern matching.

    Basically, you can use the ? character to make the parameter optional.

    /articles/:year?/:month?/:day?
    

提交回复
热议问题