【问题标题】:Node JS API responds nothing on increasing request params and keeps sending the request to the serverNode JS API 对增加的请求参数没有响应,并不断向服务器发送请求
【发布时间】:2022-02-22 16:17:31
【问题描述】:

我正在使用以下无响应的 api http://localhost:6150/api/v1/simpleSurveyData/abc/:projectId/:startDate/:endDate/:visitMonth

但是,当我删除四个参数中的任何一个或提供少于四个参数时。并相应地调整节点js中的api路由服务器开始响应。

我发现将参数增加到四个,我的 Node JS 代码没有进入 api 路由但是虽然服务器没有给出错误但请求只是发送。

router.get("api/v1/simpleSurveyData/abc/:projectId/:startDate/:endDate/:visitMonth",
  async (req, res, next) => {
    console.log("first")

    try {
      console.log("first")
 
      res.status(200).send("response");
    } catch (error) {
      next(error);
      console.log(error);
    }
  }
);

【问题讨论】:

  • 请提供更多信息。你在使用 express js 还是什么?也请出示代码
  • 我正在使用快递
  • 您如何以及从何处调用 GET 请求?
  • 我也同时使用邮递员和浏览器进行了调用。我很好奇为什么当我删除四个 req 参数中的任何一个时我的代码开始工作并且所有控制台日志都开始工作

标签: javascript mysql node.js mongodb express


【解决方案1】:

问题是因为您从 url 路径而不是查询参数中提取数据。请使用查询参数。示例代码如下。

"api/v1/simpleSurveyData/abc/:projectId/:startDate/:endDate/:visitMonth" 应该是"api/v1/simpleSurveyData/abc?projectId=<projectId>&startDate=<startDate>&endDate=<endDate>&visitMonth=<visitMonth>"

快速控制器示例

export async function upvoteQuestion(req: Request, res: Response, next: NextFunction): Promise<void> {
    try {
        const questionId: string = req.params.question_id
        await db<Question>('question').where('id', questionId).increment('upvote', 1)
        res.status(200).json(<GeneralResponse>{
            status: responseStatus.success,
            message: 'Successfully vote up question'
        })
    } catch (error: unknown) {
        next(error)
    }
}

我使用req.params.question_id提取url参数中问题id的信息。

您可以谷歌了解更多信息。

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多