Nodejs - 第一个参数必须是字符串或缓冲区



我一直收到这个匿名错误。有人知道如何解决这个问题吗?它执行http请求,但似乎在某个地方我想念JSON.stringify。

应用.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();
}
});
}
});

这是一种奇怪的行为,http.createServer的路线/favicon.ico。我只需要赶上那条路线。对我有用的是:

if (req.url === '/favicon.ico') {
res.end();
return;
}

http.createServer方法的第一行中。

最新更新