【问题标题】:How to create an api in express with route param and query param如何使用路由参数和查询参数在 express 中创建 api
【发布时间】:2020-04-08 10:22:11
【问题描述】:

我的应用程序在 nodejs 中,带有 express。

我正在尝试构建一个同时具有路由参数和查询参数的 API

以下是我尝试过的事情

 using **=**

 app.get('/:accountId/accounts/password/update?uniqueCode={uniqueCode}', async function(req, res) {
         //my code here
    }

app.get('/:accountId/accounts/password/update?uniqueCode/:uniqueCode', async function(req, res) {
             //my code here
   }

但是当我像下面这样从邮递员那里打这个时

http://localhost:5000/722/account/password/update?uniqueCode={dfgsfksjfksdhfksj}

在我尝试过的两种方式中,我都收到了来自 express 的 NOTFOUND 错误。任何人都可以建议我如何做到这一点。

【问题讨论】:

    标签: node.js express routes routeparams


    【解决方案1】:

    您必须检查代码中的 queryParams :

    app.get('/:accountId/accounts/password/update', async function(req, res, next) {
              const accountId = req.params.accoundId;
              const  uniqueCode = req.query.uniqueCode;
             ...
              if (/* checkuniqueCode is not valid */) {
                   return next()
               }
    
     }
    

    这是文档:https://expressjs.com/fr/api.html#req.query

    【讨论】:

    • 正是我需要的,我知道如何访问查询,但是 get 中的 API 是如何构建的,我没有找到。这行得通。
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多