Node https get 请求超时

2016-11-01 23:55:30 +08:00
 justrun

测试 google cse 的 api ,在 chrome 和 postman 中都能在 1s 左右获取到返回的 json (网络畅通,未科学上网),可是在 Node 中使用 https 的代码请求就一直是超时状态,请问下有可能哪里出了问题?新人不太懂,谢谢!

Node 中的代码, key 和 id 替换掉了:

var options = {
  hostname: 'www.googleapis.com',
  port: null,
  path: '/customsearch/v1?key=我的 key&cx=我的 id&num=10&q=python',
  method: 'GET'
};

var req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', (e) => {
  console.error(e);
});

报错 log :

{ [Error: connect ETIMEDOUT 172.217.26.106:443]
  code: 'ETIMEDOUT',
  errno: 'ETIMEDOUT',
  syscall: 'connect',
  address: '172.217.26.106',
  port: 443 }
531 次点击
所在节点    问与答
2 条回复
justrun
2016-11-02 00:24:16 +08:00
写了几句 python 试了下也能正常得到响应数据。。
xxxyyy
2016-11-02 00:40:54 +08:00
是否在 hosts 里绑定了 www.googleapis.com
自己 ping 172.217.26.106 可以 ping 的通吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/317252

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX