javascript 节点中的 req.body 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13954152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:17:41  来源:igfitidea点击:

What is req.body in node?

javascriptnode.jsexpress

提问by Anshul

I am learning nodejs and express, during my studies I don't understand various uses of reqsuch as:

我正在学习 nodejs 并表示,在我的学习期间,我不了解req诸如以下的各种用途:

var id = req.params.id;
var wine = req.body;

With my reqobject I think am acessing body, params.id, but I do not understand how this works and what result I should expect. Can anyone explain this code in a simpler step by step manner so i can make sense of it ?

对于我的req对象,我认为正在访问body, params.id,但我不明白这是如何工作的以及我应该期待什么结果。谁能以更简单的分步方式解释这段代码,以便我理解它?

Below is some example code where the above is used :

以下是使用上述内容的一些示例代码:

 exports.addDoctor = function(req,res){
        var doctor = req.body;
        console.log(doctor);
            db.collection('doctors',function(err,collection){
            collection.insert(doctor,{safe:true},function(err,result){
                if (err) {
                    res.send({'error':'An error is occured'});
                } else {
                    console.log('Success: ' + JSON.stringify(result[0]));
                    res.send(result[0]);
                }
            });
        });
    }

In the above example console.log(doctor)only returns {}. Why is this and is it because I have missed something else within the code specifically involving 'req'?

在上面的例子中console.log(doctor)只返回{}. 为什么会这样,是因为我错过了代码中专门涉及“req”的其他内容吗?

回答by August Lilleaas

reqis the request object. It's documented here: http://expressjs.com/api.html#req.params

req是请求对象。它记录在这里:http: //expressjs.com/api.html#req.params

回答by axel.michel

req is an object containing information about the HTTP request that raised the event. There is a simular Question (node.js what is res and req in expressjs?), have a look at the answers there.

req 是一个对象,包含有关引发事件的 HTTP 请求的信息。有一个类似的问题(node.js 什么是 expressjs 中的 res 和 req?),看看那里的答案。