【问题标题】:express cant find route快递找不到路线
【发布时间】:2020-11-11 02:39:17
【问题描述】:

我有一个带有 express 服务器的 node.js 后端。

我有名为 Templete.rpoutes.js 的路线:

const express = require("express");
const templateCtrl = require("../controllers/template.controller");

const router = express.Router();

router.route("/createtemplate").post(templateCtrl.createTemplate);
router.route("/uploadFinalTemplate").post(templateCtrl.uploadFinalTemplate);

// beneath are the routes with issue
router.route("/:templateId/productInfos").get(templateCtrl.productInfos);
router.route("/:templateId").get(templateCtrl.Read);

// router.param("templateId", templateCtrl.templateByID);

module.exports = router;

我在模板控制器中有定义的方法,我只是在记录东西。还没有后端逻辑。所以我希望当我调用这些端点时,我应该显示这些日志

我已将快递服务器配置为:

app.use("/api/template", templateRoute);
app.use("/api/productInfo", productInfoRoute);
app.use('/api/sendInformation', Notify)

// PORT
const port = process.env.PORT || 5000;
const server = app.listen(port, () => {
  console.log("Connected to port " + port);
});

注意:该应用程序是 express.router() 方法,并且需要正确的控制器,并且其他端点适用于同一控制器。

所以当我从邮递员向localhost:5000/api/template/5f870cd3b02fd77d0a576a54 发送一个获取请求时,我得到一个 404 并且在我的控制台中找不到像这样:(加上服务器正在运行)

【问题讨论】:

  • 尝试将 router.route("/createtemplate"). 替换为 router.post('/createtemplate', templateCtrl.createTemplate),其他人也一样

标签: javascript node.js express


【解决方案1】:

如文档https://expressjs.com/en/guide/routing.html#express-router中所述

const router = express.Router();

router.post("/createtemplate",templateCtrl.createTemplate);
router.post("/uploadFinalTemplate",templateCtrl.uploadFinalTemplate);

router.get("/:templateId/productInfos",templateCtrl.productInfos);
router.get("/:templateId",templateCtrl.Read);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2016-07-03
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多