11

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

1
  • That depends on the format the parameter is sent in. For JSON or URL-encoded, req.body. Note the dependency on a middleware. Commented Nov 18, 2014 at 4:35

6 Answers 6

16

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

eg:

router.delete('/test',function(req,res){
     res.send(req.body.data);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This got me half way there. I included a bit more detail below.
5

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.

Comments

1

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.");
}

Comments

1

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.

Comments

0

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

Comments

0

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),
  },
});
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.