Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

410
Views
How to modularize a delete route with params in Express

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);

  }
});

/**
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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"))

// ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!