如何在Express中使用参数模块化删除路由

发布于 2025-01-31 04:05:50 字数 1923 浏览 1 评论 0原文

我正在尝试在我的express.js应用程序中达到删除路线,但由于某种原因,它无法正常工作。

我设置路由器文件和侦听器中的请求参数的方式存在一些问题。

我尝试了各种变体,看不到我在做什么错。谢谢您的任何指示!

客户端请求中的URL是:

delete:“ localhost:8080/api/tasks/1”

路由模块的结构就像:

  1. 捕获以“ 文件中
  2. API“在 app.js 路由/
  3. 任务

。 JS,有一条路线:

router.delete("/:id, async (req, res, next)=>{
     ISSUE -----> this route never gets hit
})

更多详细信息: 路由文件是这样的:

/app.js

app.use("/api", require("./routes/index.js"));

/routes/index.js

const express = require("express");
const router = express.Router();
module.exports = router;


/**
 * @route /api/tasks
 * @verb get
 */
router.use("/tasks", require("./tasks"));


/途径/tasks/index.js

const express = require("express");
const router = express.Router();
module.exports = router;

/**
 * @route /api/tasks
 * @verb post
 */
router.post("/", require("./create"));

/**
 * @route /api/tasks
 * @verb get
 */

router.get("/", require("./read"));

/**
 * @route /api/tasks
 * @verb put
 */

router.put("/", require("./update"));

/**
 * @route /api/tasks
 * @verb delete
 */

router.delete("/", require("./delete"));



/routes/tasks/delete.js

const express = require("express");
const router = express.Router();
const db = require("../../models/db");
const { User, Task } = db.models;

module.exports = router;


router.delete("/:id", async (req, res, next) => {

  let { id } = req.params;


  // ISSUE: THIS ROUTE DOES NOT GET HIT

  console.log("hit delete route");

  try {

    res.json({ test, "hit the delete route", id });

  } catch (error) {

    next(error);

  }
});

/**

I am trying to hit a delete route in my Express.js app, and it is not working for some reason.

There is some issue with the way that I am setting up my router files and the request params in the listener.

I've tried all sorts of variations, and can't see what I'm doing wrong. Thank you for any pointers!

the url in the request from the client is:

DELETE: "localhost:8080/api/tasks/1"

the structure of the route modules is like this:

  1. catch all of the urls that start with "/api" in the app.js file and send them to routes/index.js
  2. in routes/index, send all of the urls that start with "api/tasks" to routes/tasks.js
  3. in routes/task.js send all of the requests that have a delete verb /routes/tasks/delete.js

in routes/tasks/delete.js, there is a route:

router.delete("/:id, async (req, res, next)=>{
     ISSUE -----> this route never gets hit
})

More details:
the route files are like this:

/app.js

app.use("/api", require("./routes/index.js"));

/routes/index.js

const express = require("express");
const router = express.Router();
module.exports = router;


/**
 * @route /api/tasks
 * @verb get
 */
router.use("/tasks", require("./tasks"));


/routes/tasks/index.js

const express = require("express");
const router = express.Router();
module.exports = router;

/**
 * @route /api/tasks
 * @verb post
 */
router.post("/", require("./create"));

/**
 * @route /api/tasks
 * @verb get
 */

router.get("/", require("./read"));

/**
 * @route /api/tasks
 * @verb put
 */

router.put("/", require("./update"));

/**
 * @route /api/tasks
 * @verb delete
 */

router.delete("/", require("./delete"));



/routes/tasks/delete.js

const express = require("express");
const router = express.Router();
const db = require("../../models/db");
const { User, Task } = db.models;

module.exports = router;


router.delete("/:id", async (req, res, next) => {

  let { id } = req.params;


  // ISSUE: THIS ROUTE DOES NOT GET HIT

  console.log("hit delete route");

  try {

    res.json({ test, "hit the delete route", id });

  } catch (error) {

    next(error);

  }
});

/**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2025-02-07 04:05:50

您有两个冲突

  1. app.use(...)用于加载导出的路由器

  2. 您加载单个路由的方式不正确,它们不会在导出的路线中加载,而是将接收功能(在许多框架中也称为“控制器”)

// file 1 ./deleteController.js
const deleteController = (req, res, next) => {
  // ...
}

module.exports = deleteController
// file 2 ./deleteRoutes.js
const express = require('express')
const router = express.Router()
const deleteController = require('./deleteController')

router.delete('/:id', deleteController)

module.exports = router
// file 3 ./index.js
// ...

app.use("/someNamespace", require("./deleteRoutes"))

// ...

you have two conflicts

  1. app.use(...) is for loading an exported router

  2. the way you are loading individual routes is incorrect, they will not load in an exported route, they will instead take in a function (also called a "controller" in many frameworks)

// file 1 ./deleteController.js
const deleteController = (req, res, next) => {
  // ...
}

module.exports = deleteController
// file 2 ./deleteRoutes.js
const express = require('express')
const router = express.Router()
const deleteController = require('./deleteController')

router.delete('/:id', deleteController)

module.exports = router
// file 3 ./index.js
// ...

app.use("/someNamespace", require("./deleteRoutes"))

// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文