【问题标题】:Nodejs - First argument must be a string or BufferNodejs - 第一个参数必须是字符串或缓冲区
【发布时间】: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


【解决方案1】:

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

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

http.createServer 方法内的第一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-17
    • 2022-07-14
    • 2019-02-17
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 2017-12-14
    • 2023-03-03
    相关资源
    最近更新 更多