Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

412
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda