Node.js 使用Node和Nginx的ajaxpost500内部服务器错误
我试图发布到我的节点服务器,但我得到一个“加载资源失败”的500内部服务器错误代码,我不知道为什么 下面是关于节点所说内容的示例 我的服务器代码:Node.js 使用Node和Nginx的ajaxpost500内部服务器错误,node.js,ajax,Node.js,Ajax,我试图发布到我的节点服务器,但我得到一个“加载资源失败”的500内部服务器错误代码,我不知道为什么 下面是关于节点所说内容的示例 我的服务器代码: var db = null var app = null //console.error(config.mongohq) mongo.init( { name: config.mongohq.name, host: config.mongohq.host, port: config.mong
var db = null
var app = null
//console.error(config.mongohq)
mongo.init(
{
name: config.mongohq.name,
host: config.mongohq.host,
port: config.mongohq.port,
username: config.mongohq.username,
password: config.mongohq.password,
},
function(res){
db = res
var prefix = '/assignment2/'
app = express()
// Configuration
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing
//multer_upload = multer({ dest: './images' }).any();
app.get(prefix + 'search/:query', search);
app.post(prefix + ":chicken/log", log)
app.listen(3009)
console.error('Server listening on port 3009')
},
function(err){
console.error(err)
}
)
function log(req,res)
{
var list = JSON.parse(req.body.items);
console.log(hello)
for (var i = 0; i <list.length; i++)
{
fs.appendFile("assignment2/Foghorn/log.dat", JSON.stringify(list[i]));
}
}
出于安全原因隐藏了我的ip和端口
我非常确定我的log.dat文件位于Nginx的html文件夹中的assignment2/Foghorn中
根据node,错误指向log()方法中的第1行
知道问题出在哪里吗?代码中有问题的一行是
var list=JSON.parse(req.body.items)
但是req.body.items
不是一个文本字符串,它是一个包含body参数的对象。因此,JSON.parse当然会拒绝它
JSON解析器中间件(
app.use(bodyParser.JSON())
)解析请求中的JSON,并将结果放入req.body
。所以,您不需要再次解析它 我明白了,可能是重复的。我是新手,所以我没有意识到这一点。我会尝试你的建议不知道你为什么被否决,但这解决了我的问题。有些人在知道答案并相信答案显而易见的情况下否决了问题和答案。不要担心否决票。
//Clear the local storage and array of the user's choice of chicken type
var server = "http://xxxx:xxxx/assignment2/";
function sendLogs()
{
clearFields();
var path;
if(chickenNumber == 0)
{
var items = {logs: foghorn_items};
path = server + "Foghorn/log";
$.ajax({
url: path,
method: 'POST',
dataType: 'JSON',
data: items,
success: function (data){
alert('Logs sent to file');
}
});
// foghorn_items.length = 0;
// localStorage.removeItem("foghorn_items");
}