headers: nothing special
body: pidr=radik
headers: nothing special
body: pidr=radik
将请求正文内容打印到控制台中。
Varibale [body]自初始化以来没有改变,因为它似乎。
我已经根据我的要求获得了请求正文内容,这在不改变正文(变量)的情况下是不可能实现的
const app = require("express")();
let body = "not as expected";
app.post("/radikloh", (req,res)=>{
req.on("data",function(chunk){
body = chunk.toString();
});
console.log(body)//"not as expected"
req.on("end",function(){
res.send(body);
});
console.log(body)//"not as expected"
});
app.listen(process.env.PORT);
headers: nothing special
body: pidr=radik
headers: nothing special
body: pidr=radik
将请求正文内容打印到控制台中。
正如预期的那样
const app = require("express")();
let body = "not as expected";
function buff (input){
body = input;
}
app.post("/radikloh", (req,res)=>{
req.on("data",function(chunk){
body = chunk.toString();
buff(body);
});
console.log(body)//"pidr=radik"
req.on("end",function(){
res.send(body);
});
console.log(body)//"pidr=radik"
});
app.listen(process.env.PORT);
我认为这是因为回调函数范围,但它在此示例中工作正常:
function a (cb){
cb("It worked just FINE");
}
function b (){
let body = "not as expected";
a(function(seter){
body = seter;
});
console.log(body);//THE OUTPUT: "It worked just FINE"
}
即便如此:
let body = "not as expected";
function a (cb){
cb("It worked just FINE");
}
function b (){
a(function(seter){
body = seter;
});
console.log(body);//THE OUTPUT: "It worked just FINE"
}
答案 0 :(得分:0)
您可以在节点网站上查看request body
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
// at this point, `body` has the entire request body stored in it as a string
});
您在发出POST请求时是否正在发送数据?如果身体保持不变,那么它可能没有进入req.on('data'.. function