阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js http2stream.respondWithFile

      Node.js http2stream.respondWithFile(path[, headers[, options]])

      版本历史

      版本变更
      v10.0.0Any readable file, not necessarily a regular file, is supported now.
      v8.4.0新增于: v8.4.0
      • path < string> | < Buffer> | < URL>
      • headers < HTTP/2 Headers Object>
      • options < Object>
        • statCheck < Function>
        • onError < Function> Callback function invoked in the case of an error before send.
        • waitForTrailers < boolean> When true, the Http2Stream will emit the ‘wantTrailers’ event after the final DATA frame has been sent.
        • offset < number> The offset position at which to begin reading.
        • length < number> The amount of data from the fd to send.

      Sends a regular file as the response. The path must specify a regular file or an ‘error’ event will be emitted on the Http2Stream object.

      When used, the Http2Stream object’s Duplex interface will be closed automatically.

      The optional options.statCheck function may be specified to give user code an opportunity to set additional content headers based on the fs.Stat details of the given file:

      If an error occurs while attempting to read the file data, the Http2Stream will be closed using an RST_STREAM frame using the standard INTERNAL_ERROR code. If the onError callback is defined, then it will be called. Otherwise the stream will be destroyed.

      Example using a file path:

      const http2 = require('http2');
      const server = http2.createServer();
      server.on('stream', (stream) => {
        function statCheck(stat, headers) {
          headers['last-modified'] = stat.mtime.toUTCString();
        }
      
        function onError(err) {
          if (err.code === 'ENOENT') {
            stream.respond({ ':status': 404 });
          } else {
            stream.respond({ ':status': 500 });
          }
          stream.end();
        }
      
        stream.respondWithFile('/some/file',
                               { 'content-type': 'text/plain' },
                               { statCheck, onError });
      });
      

      The options.statCheck function may also be used to cancel the send operation by returning false. For instance, a conditional request may check the stat results to determine if the file has been modified to return an appropriate 304 response:

      const http2 = require('http2');
      const server = http2.createServer();
      server.on('stream', (stream) => {
        function statCheck(stat, headers) {
          // Check the stat here...
          stream.respond({ ':status': 304 });
          return false; // Cancel the send operation
        }
        stream.respondWithFile('/some/file',
                               { 'content-type': 'text/plain' },
                               { statCheck });
      });
      

      The content-length header field will be automatically set.

      The offset and length options may be used to limit the response to a specific range subset. This can be used, for instance, to support HTTP Range requests.

      The options.onError function may also be used to handle all the errors that could happen before the delivery of the file is initiated. The default behavior is to destroy the stream.

      When the options.waitForTrailers option is set, the ‘wantTrailers’ event will be emitted immediately after queuing the last chunk of payload data to be sent. The http2stream.sendTrilers() method can then be used to sent trailing header fields to the peer.

      When options.waitForTrailers is set, the Http2Stream will not automatically close when the final DATA frame is transmitted. User code must call either http2stream.sendTrailers() or http2stream.close() to close the Http2Stream.

      const http2 = require('http2');
      const server = http2.createServer();
      server.on('stream', (stream) => {
        stream.respondWithFile('/some/file',
                               { 'content-type': 'text/plain' },
                               { waitForTrailers: true });
        stream.on('wantTrailers', () => {
          stream.sendTrailers({ ABC: 'some value to send' });
        });
      });
      

      更多内容请参考:Node.js HTTP2,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录