代码之家  ›  专栏  ›  技术社区  ›  MarcoLe

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

  •  0
  • MarcoLe  · 技术社区  · 6 年前

    我一直在犯这个错误。有人知道怎么解决这个问题吗?它执行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();
                }
            });
        }
    });
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   MarcoLe    6 年前

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

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

    在第一行 http.createserver服务器 方法。