當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js ServerHttp2Session.origin(...origins)用法及代碼示例

serverhttp2session.origin(...origins)

添加於:v10.12.0

參數

向連接的客戶端提交 ORIGIN 幀(由 RFC 8336 定義)以通告服務器能夠提供權威響應的源集。

const http2 = require('node:http2');
const options = getSecureOptionsSomehow();
const server = http2.createSecureServer(options);
server.on('stream', (stream) => {
  stream.respond();
  stream.end('ok');
});
server.on('session', (session) => {
  session.origin('https://example.com', 'https://example.org');
});

當字符串作為 origin 傳遞時,它將被解析為 URL 並派生源。例如,HTTP URL 'https://example.org/foo/bar' 的來源是 ASCII 字符串 'https://example.org' 。如果給定的字符串無法解析為 URL 或無法派生有效的來源,則會引發錯誤。

URL 對象或具有 origin 屬性的任何對象都可以作為 origin 傳遞,在這種情況下,將使用 origin 屬性的值。 origin 屬性的值必須是正確序列化的 ASCII 源。

或者,當使用 http2.createSecureServer() 方法創建新的 HTTP/2 服務器時,可以使用 origins 選項:

const http2 = require('node:http2');
const options = getSecureOptionsSomehow();
options.origins = ['https://example.com', 'https://example.org'];
const server = http2.createSecureServer(options);
server.on('stream', (stream) => {
  stream.respond();
  stream.end('ok');
});

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 ServerHttp2Session.origin(...origins)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。