我正在创建一个简单的NodeJS服务器,该服务器将接收POST请求并执行一些业务逻辑
IEnumerator MyRoutine() {
while (true) {
if (conditionA || conditionB) {
yield break; // stop stepping this
}
yield return null; // continue stepping next frame
}
}
现在,当我用json对邮递员的示例POST请求进行测试时,我得到了错误var server = http.createServer(function(request, response) {
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
try {
var post = JSON.parse(body);
deal_with_post_data(request,post);
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
return;
}catch (err){
response.writeHead(500, {"Content-Type": "text/plain"});
response.write("Bad Post Data. Is your data a proper JSON?\n");
response.end();
return;
}
});
}
});
server.listen(3000);
console.log("server started")
,这实际上意味着它进入了Bad Post Data. Is your data a proper JSON?
。
这是我尝试过的示例JSON
500 internal server error
并将POST请求发送到{
"glossary":"book"
}
有人可以建议吗?
答案 0 :(得分:1)
只需测试您的代码:
const http = require('http');
var server = http.createServer(function(request, response) {
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
try {
var post = JSON.parse(body);
// deal_with_post_data(request,post);
console.log(post); // <--- here I just output the parsed JSON
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
return;
}catch (err){
response.writeHead(500, {"Content-Type": "text/plain"});
response.write("Bad Post Data. Is your data a proper JSON?\n");
response.end();
return;
}
});
}
});
server.listen(3000);
console.log("server started")
在Postman中,我使用给定的JSON(如raw
发出了POST请求,并在控制台上记录了正确的JSON。因此提供的代码很好。问题可能出在deal_with_post_data
函数(您在此处未显示)