【问题标题】:How does express match a url with the appropriate route?express 如何将 url 与适当的路由匹配?
【发布时间】:2020-04-25 12:04:09
【问题描述】:

有人可以向我指出有关 express(或任何其他 javascript 框架)如何将 URL 与路由匹配的文档吗?比如下面的请求是怎么理解的:

补丁 https://www.helloworld.com/api/patch/company/1/organization/2/unit/3

应该被定向到:

补丁 https://www.helloworld.com/api/patch/company/:company_id/organization/:organization_id/unit/:unit_id?

我找不到具体如何执行此操作的文档。

任何指针将不胜感激。谢谢!

【问题讨论】:

    标签: node.js express url path


    【解决方案1】:

    请看这里:http://expressjs.com/en/guide/routing.html#route-parameters。 Express 将路由定义中以冒号开头的项目称为“命名参数”。

    以下是文档中的一些示例(下面的第一个与您显示的 URL 最相似):

    Route path: /users/:userId/books/:bookId
    Request URL: http://localhost:3000/users/34/books/8989
    req.params: { "userId": "34", "bookId": "8989" }
    
    Route path: /flights/:from-:to
    Request URL: http://localhost:3000/flights/LAX-SFO
    req.params: { "from": "LAX", "to": "SFO" }
    
    Route path: /plantae/:genus.:species
    Request URL: http://localhost:3000/plantae/Prunus.persica
    req.params: { "genus": "Prunus", "species": "persica" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 2013-07-26
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多