http2stream.respondWithFD(fd[, headers[, options]])
版本 | 變化 |
---|---|
v14.5.0、v12.19.0 | 允許明確設置日期標題。 |
v12.12.0 |
|
v10.0.0 | 現在支持任何可讀的文件說明符,不一定是常規文件。 |
v8.4.0 | 添加於:v8.4.0 |
參數
fd
<number> | <FileHandle> 可讀文件說明符。headers
<HTTP/2 Headers Object>options
<Object>statCheck
<Function>waitForTrailers
<boolean> 當true
時,Http2Stream
將在最後的DATA
幀發送後發出'wantTrailers'
事件。offset
<number> 開始讀取的偏移位置。length
<number> 要從 fd 發送的數據量。
啟動從給定文件說明符中讀取其數據的響應。不對給定的文件說明符執行驗證。如果在嘗試使用文件說明符讀取數據時發生錯誤,將使用標準 INTERNAL_ERROR
代碼使用 RST_STREAM
幀關閉 Http2Stream
。
使用時,Http2Stream
對象的Duplex
接口會自動關閉。
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');
const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain; charset=utf-8'
};
stream.respondWithFD(fd, headers);
stream.on('close', () => fs.closeSync(fd));
});
可以指定可選的 options.statCheck
函數,以使用戶代碼有機會根據給定 fd 的 fs.Stat
詳細信息設置附加內容頭。如果提供了statCheck
函數,則http2stream.respondWithFD()
方法將執行fs.fstat()
調用以收集有關提供的文件說明符的詳細信息。
offset
和length
選項可用於將響應限製為特定範圍子集。例如,這可以用於支持 HTTP Range 請求。
文件說明符或FileHandle
在流關閉時沒有關閉,因此一旦不再需要它就需要手動關閉。不支持同時對多個流使用相同的文件說明符,這可能會導致數據丟失。支持在流完成後重新使用文件說明符。
當設置options.waitForTrailers
選項時,將在排隊要發送的最後一塊有效負載數據後立即發出'wantTrailers'
事件。然後可以使用http2stream.sendTrailers()
方法將尾隨標頭字段發送到對等方。
當設置options.waitForTrailers
時,在傳輸最後一個DATA
幀時,Http2Stream
不會自動關閉。用戶代碼必須調用 http2stream.sendTrailers()
或 http2stream.close()
來關閉 Http2Stream
。
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');
const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain; charset=utf-8'
};
stream.respondWithFD(fd, headers, { waitForTrailers: true });
stream.on('wantTrailers', () => {
stream.sendTrailers({ ABC: 'some value to send' });
});
stream.on('close', () => fs.closeSync(fd));
});
相關用法
- Node.js ServerHttp2Stream http2stream.respondWithFile(path[, headers[, options]])用法及代碼示例
- Node.js ServerHttp2Stream http2stream.respond([headers[, options]])用法及代碼示例
- Node.js http2stream.respond()用法及代碼示例
- Node.js http2stream.rstCode用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
- Node.js http2stream.setTimeout()用法及代碼示例
- Node.js http2stream.id用法及代碼示例
- Node.js http2stream.closed用法及代碼示例
- Node.js http2stream.pushAllowed用法及代碼示例
- Node.js http2stream.sentHeaders用法及代碼示例
- Node.js http2stream.endAfterHeaders用法及代碼示例
- Node.js http2stream.pending用法及代碼示例
- Node.js http2stream.headersSent用法及代碼示例
- Node.js http2stream.close()用法及代碼示例
- Node.js http2stream.state用法及代碼示例
- Node.js http2stream.session用法及代碼示例
- Node.js http2stream.destroyed用法及代碼示例
- Node.js http2stream.additionalHeaders()用法及代碼示例
- Node.js http2stream.priority()用法及代碼示例
- Node.js http2stream.sentInfoHeaders用法及代碼示例
- Node.js http2session.destroyed用法及代碼示例
- Node.js http2session.type用法及代碼示例
- Node.js http2session.ping()用法及代碼示例
- Node.js http2session.connecting用法及代碼示例
- Node.js http2session.setTimeout()用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 http2stream.respondWithFD(fd[, headers[, options]])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。