Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 如何在nodejs中设置user-agent?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试过这个,但是我收到了一个错误.

http.get({ url: 'http://www.panda.tv/ajax_chaTinfo?roomid=89757',agent: 'Mozilla/5.0' },function(res) {
    res.on('data',function(chunk) {
        doSomething();
    });
});

我阅读了api文档,但是我没有找到任何关于如何创建内容.

当我运行代码时,我收到以下错误

_http_client.js:158
    self.agent.addrequest(self,options);
               ^

TypeError: self.agent.addrequest is not a function
    at new Clientrequest (_http_client.js:158:16)
    at Object.exports.request (http.js:31:10)

    at Object.exports.get (http.js:35:21)
    at getChaTinfo (/home/zeek/Documents/pandatv/node/app.js:5:10)
    at Object.<anonymous> (/home/zeek/Documents/pandatv/node/app.js:153:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at trymoduleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)

解决方法

const options = {
  hostname: 'www.panda.tv',path: 'ajax_chaTinfo?roomid=89757',headers: { 'User-Agent': 'Mozilla/5.0' }
};

http.get(options,function(res) {
  res.on('data',function(chunk) { console.log(chunk) });
});

虑下一个npm模块requestsuperagent,它们将帮助您处理http请求.如果您需要示例,请发表评论,但我相信您会从文档中获取. Gl.

大佬总结

以上是大佬教程为你收集整理的node.js – 如何在nodejs中设置user-agent?全部内容,希望文章能够帮你解决node.js – 如何在nodejs中设置user-agent?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。