发出 API 请求时出现 404 错误 - Node.js Express

Getting a 404 Error When Making an API Request - Node.js Express

提问人:Neel Raval 提问时间:9/4/2023 最后编辑:Neel Raval 更新时间:9/4/2023 访问量:18

问:

我在 Node.js Express 应用程序中发出 API 请求时遇到 404 错误,我正在寻找有关故障排除和解决此问题的帮助。

问题描述:

我的 Node.js Express 应用程序中有一个端点,我正在尝试向此端点发出 GET 请求,以从外部 API 获取案例详细信息。这是我的代码的简化版本:/case-log/details/:caseId

app.get("/case-log/details/:caseId", async function (req, res) {
    try {
            const token = req.session.token;
            const caseId = req.params.caseId;

            const hrn = new Date().getHours();
            const mrn = new Date().getMinutes();
            const srn = new Date().getSeconds();

            console.log("Hour: ", hrn);
            console.log("Minute: ", mrn);
            console.log("Second: ", srn);
            console.log("Token: ", token);
            console.log("Case Id: ", caseId);

            if (!token) {
                  req.flash("error", "Token missing, please log in again!");
                  res.redirect("/login");
                  return;
            }

            const config = {
                  method: "get",
                  url: "ServerIP/apis/cases/details",
                  headers: {
                        'Authorization': `Bearer ${token}`,
                  },
                  data: {
                        id: caseId,
                  },
                };
            console.log("Postman Config: ", config);

            const response = await axios.request(config);
            console.log("API Response Case Details", response.data);

            const responseData = response.data;

            if (!responseData.status) {
                  req.flash("error", "Error fetching case details from the API");
                  res.redirect("/case-log");
                  return;
            }

            const caseDetails = responseData.results;
            res.render("Details/case-details", { caseDetails, title: "Case Details" });
    } catch (error) {
            console.error("Error fetching case details: ", error);
            req.flash("error", "An error occurred while fetching case details");
            res.redirect("/case-log");    }
});
node.js express axios http-status-codes

评论

0赞 TBA 9/5/2023
路由似乎很好 - 在发出 GET 请求时,您能看到打印的console.log消息吗?

答: 暂无答案