我是 Express 的新手。我有以下代碼:
const { response } = require("express");
const express = require("express");
const app = express();
app.get("/api/products/:id", function (req, res) {
const id = req.params.id;
if (id === 1) {
res.json(id);
}
});
但是,每次我設定條件時,如果服務器不回應并繼續處理。難道我做錯了什么?
uj5u.com熱心網友回復:
req.params 總是字串;
app.get("/api/products/:id", function (req, res) {
const id = req.params.id;
if (id === '1') { // look here, the single quotes around 1 -> '1'
res.json(id);
} else {
res.json({message: 'Wrong param'}) // respond also if id is not '1', otherwise the client will still wait for a response when param is different by '1'
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/525209.html
