添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
玉树临风的香菜  ·  python - Workaround ...·  1 年前    · 
很酷的生姜  ·  node.js - "Cannot use ...·  2 年前    · 
逃跑的单杠  ·  javaScript ...·  2 年前    · 

nodejs http request read timeout

在 Node.js 中,可以使用 http 或 https 模块发起 HTTP 请求。在发起请求时,可以使用 options 对象来设置超时时间。

例如,使用 http 模块发起请求:

const http = require('http');
const options = {
  hostname: 'www.example.com',
  path: '/',
  timeout: 2000
const req = http.request(options, (res) => {
  // 在这里处理响应
req.on('timeout', () => {
  console.error('请求超时');
  req.abort();
req.end();

在这个例子中,超时时间设置为 2000 毫秒,如果请求超过了设置的时间还没有得到响应,将会触发 'timeout' 事件。

对于 https 模块,使用方法基本相同。

如果需要使用其他http请求库,如axios,request等, 也可以通过相应的api来设置超时时间。

  •