I am fresher to nodejs. How to get the value parameter pass via Delete request? I am using node express js. Thanks in advs
6 Answers
You may use "req.body" to get the value of you send
eg:
router.delete('/test',function(req,res){
res.send(req.body.data);
});
1 Comment
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
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
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
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),
},
});
...
req.body. Note the dependency on a middleware.