jason
jason

Reputation: 123

How to get parameter for delete request in express node js

I am fresher to nodejs. How to get the value parameter pass via Delete request? I am using node express js. Thanks in advs

Upvotes: 11

Views: 48561

Answers (6)

Uğur Okur
Uğur Okur

Reputation: 11

frontend;

 let response;
try {
  response = await axios.delete('/api/roles/deleterole/' + id);
...

backend;

router.delete('/deleterole/:id', auth, async (req, res) => {

const {id} = req.params;

 if (!id) {
    return res.status(200).json({success: false, message: 'Bla Bla'});
  }
 await prisma.roles.delete({
  where: {
    id: parseInt(id),
  },
});
...

Upvotes: 0

jack_t
jack_t

Reputation: 163

The DELETE is a bit tricky but very crucial to understand when Express JS is used. Here is a simple example code.

var express = require("express");
var server = express();

var backlogItems = [
    {
        itemId: "DEV-3345",
        title: "Develop a proof of concept (PoC)"
    },
    {
        itemId: "DEV-3346",
        title: "Ponder the project's major milestones"
    }
];

// the short program does not include GET and POST implementation for simplicity.

// DELETE implementation

server.delete("/project/backlog/:itemId", 
                       function(req, res) 
{
    backlogItems = skierTerms.filter(function(definition) 
                   {
        return definition.itemId.toLowerCase() !== req.params.itemId.toLowerCase();
                   }); 

    res.json(backlogItems); //sending the updated response back to client app.
});

server.listen(3500);
enter code here

In server.delete(..), itemId is a place holder variable name and it always appears after : . DELETE request of backlogItem triggers callback function and backlogItems resource is updated and sent back to client in res.

Upvotes: 1

Max Sherbakov
Max Sherbakov

Reputation: 1955

my delete controller method...

exports.remove = (req, res, next) => {
    console.log(req.params.agrgtId);
    const { agr } = req.params.agrgtId;
    agr.destroy();

};

where agrgtId is variable data from DELETE query

Upvotes: 0

keithpjolley
keithpjolley

Reputation: 2263

For more details on how to get the req.body see the example here: express request body.
And documentation here: multer npm package.

To use with a router I did this in ./app.js

var multer = require('multer');
var upload = multer();
var deldev = require('./routes/deldev');
...
app.use('/deldev', upload.array(), deldev);
...

and, in ./routes/deldev.js:

router.delete('/', function(req, res, next) {
  console.log("delete: req.body: " + JSON.stringify(req.body));
  res.json(req.body);
}

// (for debug only. don't use this.)
router.all('/', function(req, res, next) {
  res.send("don't do that.");
}

Upvotes: 1

Saravanan Rajaraman
Saravanan Rajaraman

Reputation: 1033

You may use "req.body" to get the value of you send

eg:

router.delete('/test',function(req,res){
     res.send(req.body.data);
});

Upvotes: 16

Andrew Miner
Andrew Miner

Reputation: 6125

Your question is a bit unclear, but I think you're asking how/whether an HTTP request with a DELETE method can have parameters like any other request. If so, yes, it can, and in all the same ways other requests can. The most general way to get request parameters is using the param(<name>) method on the request object, but there are several others depending upon exactly how the request is formatted. Check out the documentation for more information.

Upvotes: 5

Related Questions