request流形式下载文件,文件保存不完整
发布于 1 个月前 作者 jiangli373 647 次浏览 来自 问答

使用request模块,下载服务器上的文件,使用stream的方式下载,如果先pause下,过段时间在resume,最后下载下来的文件 不完整,服务器上的文件是正常的

代码如下所示:

'use strict';
const request = require('request');
const timeout = 1000 * 60 * 3;
const fs = require('fs');
const path = require('path');
const sss = path.join(__dirname, '123123213.dmg');
const url = 'http://p2oryw9jc.bkt.clouddn.com/charles-proxy-4.1.4.dmg';
let inStream = request.get(url);
inStream.pause();
inStream.pipe(fs.createWriteStream(sss));
setTimeout(() => {
  inStream.resume()
}, timeout)

请问这是原因造成的呢,谢谢大家了

6 回复

我没有尝试过类似的事情,但是是否可以监听一下response事件或者error时间,打印一下? 类似: inStream .on(‘response’, function(response) { console.log(response); }) .on(‘error’, function(err) { console.log(err); });

@guo-q

data 事件 会接受到部分数据

error 事件

{ Error: read ECONNRESET
    at exports._errnoException (util.js:1022:11)
    at TCP.onread (net.js:610:25) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

我感觉貌似是请求断开了 response 事件

 IncomingMessage {
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     destroyed: false,
     defaultEncoding: 'utf8',
     awaitDrain: 0,
     readingMore: true,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events:
   { end: [ [Function: responseOnEnd], [Function], [Object] ],
     close: [Function] },
  _eventsCount: 2,
  _maxListeners: undefined,
  socket:
   Socket {
     connecting: false,
     _hadError: false,
     _handle:
      TCP {
        bytesRead: 821,
        _externalStream: [External],
        fd: 14,
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        writeQueueSize: 0 },
     _parent: null,
     _host: 'p2oryw9jc.bkt.clouddn.com',
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [Object],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events:
      { end: [Array],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        free: [Function: onFree],
        close: [Array],
        agentRemove: [Function: onRemove],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener],
        data: [Function: socketOnData] },
     _eventsCount: 9,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     allowHalfOpen: false,
     _bytesDispatched: 93,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     parser:
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': null,
        _headers: [],
        _url: '',
        _consumed: false,
        socket: [Circular],
        incoming: [Circular],
        outgoing: [Object],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage:
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Circular],
        connection: [Circular],
        _header: 'GET /charles-proxy-4.1.4.dmg HTTP/1.1\r\nhost: p2oryw9jc.bkt.clouddn.com\r\nConnection: close\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'GET',
        path: '/charles-proxy-4.1.4.dmg',
        _ended: false,
        res: [Circular],
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: [Object],
        maxHeadersCount: null,
        [Symbol(outHeadersKey)]: [Object] },
     read: [Function],
     _consuming: true,
     [Symbol(asyncId)]: 8,
     [Symbol(bytesRead)]: 0 },
  connection:
   Socket {
     connecting: false,
     _hadError: false,
     _handle:
      TCP {
        bytesRead: 821,
        _externalStream: [External],
        fd: 14,
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        writeQueueSize: 0 },
     _parent: null,
     _host: 'p2oryw9jc.bkt.clouddn.com',
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [Object],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events:
      { end: [Array],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        free: [Function: onFree],
        close: [Array],
        agentRemove: [Function: onRemove],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener],
        data: [Function: socketOnData] },
     _eventsCount: 9,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     allowHalfOpen: false,
     _bytesDispatched: 93,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     parser:
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': null,
        _headers: [],
        _url: '',
        _consumed: false,
        socket: [Circular],
        incoming: [Circular],
        outgoing: [Object],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage:
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Circular],
        connection: [Circular],
        _header: 'GET /charles-proxy-4.1.4.dmg HTTP/1.1\r\nhost: p2oryw9jc.bkt.clouddn.com\r\nConnection: close\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'GET',
        path: '/charles-proxy-4.1.4.dmg',
        _ended: false,
        res: [Circular],
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: [Object],
        maxHeadersCount: null,
        [Symbol(outHeadersKey)]: [Object] },
     read: [Function],
     _consuming: true,
     [Symbol(asyncId)]: 8,
     [Symbol(bytesRead)]: 0 },
  httpVersionMajor: 1,
  httpVersionMinor: 1,
  httpVersion: '1.1',
  complete: false,
  headers:
   { server: 'NWS_TCloud_SOC1D',
     connection: 'keep-alive',
     date: 'Tue, 23 Jan 2018 02:39:32 GMT',
     'cache-control': 'public, max-age=31536000',
     expires: 'Wed, 23 Jan 2019 02:39:32 GMT',
     'last-modified': 'Wed, 17 Jan 2018 06:02:08 GMT',
     'content-type': 'application/x-apple-diskimage',
     'content-length': '58845833',
     'x-nws-log-uuid': '71c15b0e-f12e-476e-b269-81105f056544 0c2871257850ac0afafd12d27e322d4b',
     'x-cache-lookup': 'Hit From Disktank3, Hit From Upstream, Hit From Inner Cluster, Hit From Upstream',
     'access-control-allow-origin': '*',
     'access-control-max-age': '2592000',
     'accept-ranges': 'bytes',
     'content-transfer-encoding': 'binary',
     'x-reqid': 'MwsAAHbAs4-sgwoV',
     'content-disposition': 'inline; filename="charles-proxy-4.1.4.dmg"; filename*=utf-8\' \'charles-proxy-4.1.4.dmg',
     'x-daa-tunnel': 'hop_count=3' },
  rawHeaders:
   [ 'Server',
     'NWS_TCloud_SOC1D',
     'Connection',
     'keep-alive',
     'Date',
     'Tue, 23 Jan 2018 02:39:32 GMT',
     'Cache-Control',
     'public, max-age=31536000',
     'Expires',
     'Wed, 23 Jan 2019 02:39:32 GMT',
     'Last-Modified',
     'Wed, 17 Jan 2018 06:02:08 GMT',
     'Content-Type',
     'application/x-apple-diskimage',
     'Content-Length',
     '58845833',
     'X-NWS-LOG-UUID',
     '71c15b0e-f12e-476e-b269-81105f056544 0c2871257850ac0afafd12d27e322d4b',
     'X-Cache-Lookup',
     'Hit From Disktank3',
     'Access-Control-Allow-Origin',
     '*',
     'Access-Control-Max-Age',
     '2592000',
     'Accept-Ranges',
     'bytes',
     'Content-Transfer-Encoding',
     'binary',
     'X-ReqId',
     'MwsAAHbAs4-sgwoV',
     'Content-Disposition',
     'inline; filename="charles-proxy-4.1.4.dmg"; filename*=utf-8\' \'charles-proxy-4.1.4.dmg',
     'X-Daa-Tunnel',
     'hop_count=3',
     'X-Cache-Lookup',
     'Hit From Upstream',
     'X-Cache-Lookup',
     'Hit From Inner Cluster',
     'X-Cache-Lookup',
     'Hit From Upstream' ],
  trailers: {},
  rawTrailers: [],
  upgrade: false,
  url: '',
  method: null,
  statusCode: 200,
  statusMessage: 'OK',
  client:
   Socket {
     connecting: false,
     _hadError: false,
     _handle:
      TCP {
        bytesRead: 821,
        _externalStream: [External],
        fd: 14,
        reading: true,
        owner: [Circular],
        onread: [Function: onread],
        onconnection: null,
        writeQueueSize: 0 },
     _parent: null,
     _host: 'p2oryw9jc.bkt.clouddn.com',
     _readableState:
      ReadableState {
        objectMode: false,
        highWaterMark: 16384,
        buffer: [Object],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: true,
        ended: false,
        endEmitted: false,
        reading: false,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        resumeScheduled: false,
        destroyed: false,
        defaultEncoding: 'utf8',
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events:
      { end: [Array],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        free: [Function: onFree],
        close: [Array],
        agentRemove: [Function: onRemove],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener],
        data: [Function: socketOnData] },
     _eventsCount: 9,
     _maxListeners: undefined,
     _writableState:
      WritableState {
        objectMode: false,
        highWaterMark: 16384,
        finalCalled: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        destroyed: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        corked: 0,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function: bound onwrite],
        writecb: null,
        writelen: 0,
        bufferedRequest: null,
        lastBufferedRequest: null,
        pendingcb: 0,
        prefinished: false,
        errorEmitted: false,
        bufferedRequestCount: 0,
        corkedRequestsFree: [Object] },
     writable: true,
     allowHalfOpen: false,
     _bytesDispatched: 93,
     _sockname: null,
     _pendingData: null,
     _pendingEncoding: '',
     server: null,
     _server: null,
     parser:
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': null,
        _headers: [],
        _url: '',
        _consumed: false,
        socket: [Circular],
        incoming: [Circular],
        outgoing: [Object],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage:
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Circular],
        connection: [Circular],
        _header: 'GET /charles-proxy-4.1.4.dmg HTTP/1.1\r\nhost: p2oryw9jc.bkt.clouddn.com\r\nConnection: close\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'GET',
        path: '/charles-proxy-4.1.4.dmg',
        _ended: false,
        res: [Circular],
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: [Object],
        maxHeadersCount: null,
        [Symbol(outHeadersKey)]: [Object] },
     read: [Function],
     _consuming: true,
     [Symbol(asyncId)]: 8,
     [Symbol(bytesRead)]: 0 },
  _consuming: false,
  _dumped: false,
  req:
   ClientRequest {
     domain: null,
     _events:
      { socket: [Function],
        response: [Function: bound ],
        error: [Function: bound ],
        drain: [Function],
        prefinish: [Function: requestOnPrefinish] },
     _eventsCount: 5,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     outputSize: 0,
     writable: true,
     _last: true,
     upgrading: false,
     chunkedEncoding: false,
     shouldKeepAlive: false,
     useChunkedEncodingByDefault: false,
     sendDate: false,
     _removedConnection: false,
     _removedContLen: false,
     _removedTE: false,
     _contentLength: 0,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'p2oryw9jc.bkt.clouddn.com',
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _eventsCount: 9,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        _bytesDispatched: 93,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: [Object],
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        [Symbol(asyncId)]: 8,
        [Symbol(bytesRead)]: 0 },
     connection:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'p2oryw9jc.bkt.clouddn.com',
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _eventsCount: 9,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        _bytesDispatched: 93,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: [Object],
        _httpMessage: [Circular],
        read: [Function],
        _consuming: true,
        [Symbol(asyncId)]: 8,
        [Symbol(bytesRead)]: 0 },
     _header: 'GET /charles-proxy-4.1.4.dmg HTTP/1.1\r\nhost: p2oryw9jc.bkt.clouddn.com\r\nConnection: close\r\n\r\n',
     _onPendingData: [Function: noopPendingOutput],
     agent:
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256 },
     socketPath: undefined,
     timeout: undefined,
     method: 'GET',
     path: '/charles-proxy-4.1.4.dmg',
     _ended: false,
     res: [Circular],
     aborted: undefined,
     timeoutCb: null,
     upgradeOrConnect: false,
     parser:
      HTTPParser {
        '0': [Function: parserOnHeaders],
        '1': [Function: parserOnHeadersComplete],
        '2': [Function: parserOnBody],
        '3': [Function: parserOnMessageComplete],
        '4': null,
        _headers: [],
        _url: '',
        _consumed: false,
        socket: [Object],
        incoming: [Circular],
        outgoing: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     maxHeadersCount: null,
     [Symbol(outHeadersKey)]: { host: [Array] } },
  request:
   Request {
     domain: null,
     _events:
      { pipe: [Function],
        response: [Function],
        data: [Array],
        error: [Array],
        close: [Array],
        end: [Array] },
     _eventsCount: 6,
     _maxListeners: undefined,
     uri:
      Url {
        protocol: 'http:',
        slashes: true,
        auth: null,
        host: 'p2oryw9jc.bkt.clouddn.com',
        port: 80,
        hostname: 'p2oryw9jc.bkt.clouddn.com',
        hash: null,
        search: null,
        query: null,
        pathname: '/charles-proxy-4.1.4.dmg',
        path: '/charles-proxy-4.1.4.dmg',
        href: 'http://p2oryw9jc.bkt.clouddn.com/charles-proxy-4.1.4.dmg' },
     method: 'GET',
     readable: true,
     writable: true,
     explicitMethod: true,
     _qs:
      Querystring {
        request: [Circular],
        lib: [Object],
        useQuerystring: undefined,
        parseOptions: {},
        stringifyOptions: {} },
     _auth:
      Auth {
        request: [Circular],
        hasAuth: false,
        sentAuth: false,
        bearerToken: null,
        user: null,
        pass: null },
     _oauth: OAuth { request: [Circular], params: null },
     _multipart:
      Multipart {
        request: [Circular],
        boundary: 'be51b28c-b511-4b2e-aaab-7c0687e57f97',
        chunked: false,
        body: null },
     _redirect:
      Redirect {
        request: [Circular],
        followRedirect: true,
        followRedirects: true,
        followAllRedirects: false,
        followOriginalHttpMethod: false,
        allowRedirect: [Function],
        maxRedirects: 10,
        redirects: [],
        redirectsFollowed: 0,
        removeRefererHeader: false },
     _tunnel:
      Tunnel {
        request: [Circular],
        proxyHeaderWhiteList: [Array],
        proxyHeaderExclusiveList: [] },
     headers: {},
     setHeader: [Function],
     hasHeader: [Function],
     getHeader: [Function],
     removeHeader: [Function],
     localAddress: undefined,
     pool: {},
     dests: [ [Object] ],
     __isRequestRequest: true,
     proxy: null,
     tunnel: false,
     setHost: true,
     originalCookieHeader: undefined,
     _disableCookies: true,
     _jar: undefined,
     port: 80,
     host: 'p2oryw9jc.bkt.clouddn.com',
     path: '/charles-proxy-4.1.4.dmg',
     httpModule:
      { _connectionListener: [Function: connectionListener],
        METHODS: [Array],
        STATUS_CODES: [Object],
        Agent: [Object],
        ClientRequest: [Object],
        globalAgent: [Object],
        IncomingMessage: [Object],
        OutgoingMessage: [Object],
        Server: [Object],
        ServerResponse: [Object],
        createServer: [Function: createServer],
        get: [Function: get],
        request: [Function: request] },
     agentClass: { [Function: Agent] super_: [Object], defaultMaxSockets: Infinity },
     agent:
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: [Object],
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256 },
     _paused: true,
     _started: true,
     href: 'http://p2oryw9jc.bkt.clouddn.com/charles-proxy-4.1.4.dmg',
     req:
      ClientRequest {
        domain: null,
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        output: [],
        outputEncodings: [],
        outputCallbacks: [],
        outputSize: 0,
        writable: true,
        _last: true,
        upgrading: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        _contentLength: 0,
        _hasBody: true,
        _trailer: '',
        finished: true,
        _headerSent: true,
        socket: [Object],
        connection: [Object],
        _header: 'GET /charles-proxy-4.1.4.dmg HTTP/1.1\r\nhost: p2oryw9jc.bkt.clouddn.com\r\nConnection: close\r\n\r\n',
        _onPendingData: [Function: noopPendingOutput],
        agent: [Object],
        socketPath: undefined,
        timeout: undefined,
        method: 'GET',
        path: '/charles-proxy-4.1.4.dmg',
        _ended: false,
        res: [Circular],
        aborted: undefined,
        timeoutCb: null,
        upgradeOrConnect: false,
        parser: [Object],
        maxHeadersCount: null,
        [Symbol(outHeadersKey)]: [Object] },
     ntick: true,
     response: [Circular],
     originalHost: 'p2oryw9jc.bkt.clouddn.com',
     originalHostHeaderName: 'host',
     responseContent: [Circular] },
  toJSON: [Function: responseToJSON],
  caseless:
   Caseless {
     dict:
      { server: 'NWS_TCloud_SOC1D',
        connection: 'keep-alive',
        date: 'Tue, 23 Jan 2018 02:39:32 GMT',
        'cache-control': 'public, max-age=31536000',
        expires: 'Wed, 23 Jan 2019 02:39:32 GMT',
        'last-modified': 'Wed, 17 Jan 2018 06:02:08 GMT',
        'content-type': 'application/x-apple-diskimage',
        'content-length': '58845833',
        'x-nws-log-uuid': '71c15b0e-f12e-476e-b269-81105f056544 0c2871257850ac0afafd12d27e322d4b',
        'x-cache-lookup': 'Hit From Disktank3, Hit From Upstream, Hit From Inner Cluster, Hit From Upstream',
        'access-control-allow-origin': '*',
        'access-control-max-age': '2592000',
        'accept-ranges': 'bytes',
        'content-transfer-encoding': 'binary',
        'x-reqid': 'MwsAAHbAs4-sgwoV',
        'content-disposition': 'inline; filename="charles-proxy-4.1.4.dmg"; filename*=utf-8\' \'charles-proxy-4.1.4.dmg',
        'x-daa-tunnel': 'hop_count=3' } } }

来自酷炫的 CNodeMD

inStream.on(‘error’, () => console.log(error)), read timeout 在 linux 中一般都是 20 ~ 120s,出现这个问题一般都是服务端主动断开了, 应该就是在120s那个点左右,你可以把 timeout 设置小点, 或者服务端 timeout 设置大点

@jiangli373 实验了一下 timeout的时间变小确实可以完整下载你的内容,你出现的问题应该是在pause的时候服务器主动断开了连接,不知道你的业务场景是什么。

服务端主动断开了连接。

@guo-q @mosaic101 @JacksonTian 谢谢各位的回答了 现在的业务场景是服务器端需要做文件打包下载,然后文件都是保存在云存储上的,然后我这里选择的解决方案是 jszip + request

jszip添加stream

zip.file(‘name’, request.get(item.downLoadUrl));

然后现在遇到问题的是如果有多个文件的话,会添加多个file,就像上面的形式,但是我研究了下jszip的源码,貌似如果对多个文件的话,是先把stream放在一个数组里面的,然后依次执行,其它的stream会先pause,等前一个执行完成后,下一个stream会resume,这样会出现上面的问题。 下面是我的一个测试代码

  'use strict';
  const JSZip = require('jszip');
  const request = require('request');
  const fs = require('fs');
  const path = require('path');
  const sss = path.join(__dirname, 'me.zip');
  const resultArr = [
	{
	  fileName: 'charles-proxy-4.1.4.dmg',
	  downLoadUrl: 'http://p2oryw9jc.bkt.clouddn.com/charles-proxy-4.1.4.dmg'
	},
	{
	  fileName: 'googlechrome_mac_59.0.3071.109.dmg',
	  downLoadUrl: 'http://p2oryw9jc.bkt.clouddn.com/googlechrome_mac_59.0.3071.109.dmg'
	}
  ];
  const zip = new JSZip();
  resultArr.map(item => {
	let inStream = request.get(item.downLoadUrl);
	let name = item.fileName;
	let length = 0;
	inStream
	  .on('response', function (response) {
		console.error(name + '--content-type--', response.headers); // 'image/png'
	  })
	  .on('data', function (data) {
		length += data.length;
	  })
	  .on('error', function (e) {
		console.error(name + '---error---->', e);
	  })
	  .on('end', function () {
		console.error(name + '---end---- length:'+ length);
	  });
	zip.file(item.fileName, inStream);
  });
  const stream = zip.generateNodeStream({type: 'nodebuffer', streamFiles: true});
  stream.pipe(fs.createWriteStream(sss));
  stream
	.on('data', function (data, metadata) {
	})
	.on('error', function (e) {
	  console.error('---error-----', e);
	})
	.on('end', function () {
	  console.error('---end-----');
	});
	```
  不知道大家有什么其它的解决方案吗
回到顶部