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

时间:2018-06-29 17:42:44

标签: node.js http httprequest

我不断收到这个令人讨厌的错误。有人知道如何解决吗?它会执行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();
            }
        });
    }
});

1 个答案:

答案 0 :(得分:0)

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

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

http.createServer方法内的第一行。