node.js 如何在快速节点js中获取删除请求的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26986569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get parameter for delete request in express node js
提问by jason
I am fresher to nodejs. How to get the value parameter pass via Delete request? I am using node express js. Thanks in advs
我对 nodejs 比较新鲜。如何通过删除请求获取值参数传递?我正在使用 node express js。感谢广告
回答by Saravanan Rajaraman
You may use "req.body" to get the value of you send
您可以使用“req.body”来获取您发送的值
eg:
例如:
router.delete('/test',function(req,res){
res.send(req.body.data);
});
回答by Andrew Miner
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 requestobject, but there are several others depending upon exactly how the request is formatted. Check out the documentationfor more information.
您的问题有点不清楚,但我认为您是在问带有 DELETE 方法的 HTTP 请求如何/是否可以像任何其他请求一样具有参数。如果是这样,是的,它可以,并且其他请求也可以。获取请求参数的最通用方法是使用对象param(<name>)上的方法request,但还有其他几种方法,具体取决于请求的格式。查看文档以获取更多信息。
回答by keithpjolley
For more details on how to get the req.body see the example here:
express request body.
And documentation here:
multer npm package.
有关如何获取 req.body 的更多详细信息,请参见此处的示例:
express request body。
和这里的文档:
multer npm package。
To use with a router I did this in ./app.js
为了与路由器一起使用,我在 ./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:
并且,在 ./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.");
}
回答by Hyman_t
The DELETE is a bit tricky but very crucial to understand when Express JS is used. Here is a simple example code.
DELETE 有点棘手,但对于理解何时使用 Express JS 非常重要。这是一个简单的示例代码。
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(..), itemIdis a place holder variable name and it always appears after :. DELETE request of backlogItemtriggers callback function and backlogItemsresource is updated and sent back to client in res.
在 server.delete(..) 中,itemId是一个占位符变量名,它总是出现在:. backlogItem触发回调函数和backlogItems资源的DELETE 请求被更新并发回客户端res。
回答by Max Sherbakov
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
其中 agrgtId 是来自 DELETE 查询的变量数据

