Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

javascript - 使用Node.js和Express进行POST时如何访问请求体?(How to access the request body when POSTing using Node.js and Express?)

I have the following Node.js code:

(我有以下Node.js代码:)

var express = require('express');
var app = express.createServer(express.logger());
app.use(express.bodyParser());

app.post('/', function(request, response) {
    response.write(request.body.user);
    response.end();
});

Now if I POST something like:

(现在,如果我发布类似的东西:)

curl -d user=Someone -H Accept:application/json --url http://localhost:5000

I get Someone as expected.

(我按预期得到了Someone 。)

Now, what if I want to get the full request body?

(现在,如果我想获得完整的请求体,该怎么办?)

I tried doing response.write(request.body) but Node.js throws an exception saying " first argument must be a string or Buffer " then goes to an "infinite loop" with an exception that says " Can't set headers after they are sent. ";

(我尝试做response.write(request.body)但是Node.js抛出一个异常,说“ 第一个参数必须是一个字符串或缓冲区 ”然后进入“无限循环”,异常是“ 无法在它们之后设置标题发送。 “;)

this also true even if I did var reqBody = request.body;

(即使我做了var reqBody = request.body;这也是如此var reqBody = request.body;)

and then writing response.write(reqBody) .

(然后编写response.write(reqBody) 。)

What's the issue here?

(这是什么问题?)

Also, can I just get the raw request without using express.bodyParser() ?

(另外,我可以在不使用express.bodyParser()情况下获取原始请求吗?)

  ask by TheBlueSky translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Express 4.0 and above:(Express 4.0及以上版本:)

$ npm install --save body-parser

And then in your node app:

(然后在您的节点应用程序中:)

const bodyParser = require('body-parser');
app.use(bodyParser);

Express 3.0 and below:(Express 3.0及以下版本:)

Try passing this in your cURL call:

(尝试在你的cURL调用中传递它:)

--header "Content-Type: application/json"

and making sure your data is in JSON format:

(并确保您的数据是JSON格式:)

{"user":"someone"}

Also, you can use console.dir in your node.js code to see the data inside the object as in the following example:

(此外,您可以在node.js代码中使用console.dir来查看对象内的数据,如以下示例所示:)

var express = require('express');
var app = express.createServer();

app.use(express.bodyParser());

app.post('/', function(req, res){
    console.dir(req.body);
    res.send("test");
}); 

app.listen(3000);

This other question might also help: How to receive JSON in express node.js POST request?

(这个问题也可能会有所帮助: 如何在express node.js POST请求中接收JSON?)

If you don't want to use the bodyParser check out this other question: https://stackoverflow.com/a/9920700/446681

(如果你不想使用bodyParser,请查看另一个问题: https ://stackoverflow.com/a/9920700/446681)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...