[英]Node express request goes to other endpoint in router
我想不通。 為什么是我的要求:
localhost:3000/api/customers/search?q=glenn
前往:
// Retrieve a single Customer with customerId
router.get("/:customerId", customers.findOne);
什么時候應該go到這里???
// Search for a customer.
router.get("/search/:q", customers.search)
客戶.routes.js
module.exports = app => {
const customers = require("../controllers/customer.controller");
const router = require("express").Router();
// Create a new Customer
router.post("/", customers.create);
// Retrieve all Customers
router.get("/", customers.findAll);
// Search for a customer.
router.get("/search/:q", customers.search)
// Retrieve a single Customer with customerId
router.get("/:customerId", customers.findOne);
// Update a Customer with customerId
router.put("/:customerId", customers.update);
// Delete a Customer with customerId
router.delete("/:customerId", customers.delete);
// Create a new Customer
router.delete("/", customers.deleteAll);
app.use("/api/customers", router)
};
Morgan + Sequelize 日志:
執行(默認): SELECT
id
,name
,active
,createdAt
,updatedAt
FROMcustomers
AScustomer
WHEREcustomer
。id
= '搜索'; ::1 - - [25/Apr/2020:16:41:06 +0000] "GET /api/customers/search?q=glenn HTTP/1.1" 200 0 "-" "PostmanRuntime/7.24.1"
您的請求與路由器正在查找的內容不匹配,或者將您的請求從localhost:3000/api/customers/search?q=glenn
更改為localhost:3000/api/customers/search/glenn
或者
將router.get("/search/:q", customers.search)
更改為router.get("/search", customers.search)
你需要另一條路線來處理,搜索沒有“/:r”。
/search/:q
僅適用於查詢
/search/test
/search/something
不是
/search?q=something
更新:
// Search for a customer.
router.get("/search/:q", customers.search)
// Add this
// Search for a for query.
router.get("/search", customers.search)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.