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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…