【发布时间】:2018-06-30 15:00:40
【问题描述】:
我不断收到这个令人讨厌的错误。有人知道如何解决这个问题吗?它执行 http 请求,但在某处我似乎错过了 JSON.stringify。
app.js
let server = http.createServer(function (req, res) {
path = req.url;
if (path === undefined || path === '/') {
res.end('Welcome... all-about-clash site.');
} else {
const options = {
host: 'api.clashofclans.com',
path: path,
url: 'https://api.clashofclans.com' + path,
method: 'GET',
json: true,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token'
}
};
request(options, (error, response, body) => {
if (!error) {
res.write(JSON.stringify(body));
res.end();
} else {
res.write(JSON.stringify(error));
res.end();
}
});
}
});
【问题讨论】:
-
在您的
JSON.stringify结果中添加console.log。我打赌你的 JSON 结果是undefined或类似的东西。 -
@Chris 你赌对了 ;D
标签: node.js http httprequest