Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

  }
});

/**
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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

// ...
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda