代码之家  ›  专栏  ›  技术社区  ›  Klode

ExpressJS路由器,参数为空

  •  0
  • Klode  · 技术社区  · 6 年前

    我的路由规则之一是:

    api.get("/api/plugin/:action", (req,res) => { ... }
    

    如果我发送:

    http://localhost:3000/api/plugin
    

    我有个错误:

    Cannot get /api/plugin/
    

    2 回复  |  直到 6 年前
        1
  •  2
  •   Adeel Nawaz    6 年前

    你可以用 ? 使您的参数可选

    api.get("/api/plugin/:action?", (req,res) => { ... }
    
        2
  •  1
  •   Davit Abulashvili    6 年前

    1) 将可选路径param与*一起使用,如下所示

    api.get("/api/plugin/:action?", (req,res) => { ... }
    

    api.get("/api/plugin/", (req,res) => { ... }
    

    打电话给:

    http://localhost:3000/api/plugin?action=
    

    登记入住 对象

    推荐文章