serverhttp2session.origin(...origins)
向已连接的客户端提交一个 ORIGIN 帧(如 RFC 8336 定义),以通告服务器能够提供权威响应的一组源。
【Submits an ORIGIN frame (as defined by RFC 8336) to the connected client
to advertise the set of origins for which the server is capable of providing
authoritative responses.】
import { createSecureServer } from 'node:http2';
const options = getSecureOptionsSomehow();
const server = createSecureServer(options);
server.on('stream', (stream) => {
stream.respond();
stream.end('ok');
});
server.on('session', (session) => {
session.origin('https://example.com', 'https://example.org');
});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,或者无法得出有效的原点,将会抛出错误。
【When a string is passed as an origin, it will be parsed as a URL and the
origin will be derived. For instance, the origin for the HTTP URL
'https://example.org/foo/bar' is the ASCII string
'https://example.org'. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.】
可以将 URL 对象或任何具有 origin 属性的对象作为 origin 传入,这种情况下将使用 origin 属性的值。origin 属性的值必须是正确序列化的 ASCII 来源。
【A URL object, or any object with an origin property, may be passed as
an origin, in which case the value of the origin property will be
used. The value of the origin property must be a properly serialized
ASCII origin.】
或者,在使用 http2.createSecureServer() 方法创建新的 HTTP/2 服务器时,可以使用 origins 选项:
【Alternatively, the origins option may be used when creating a new HTTP/2
server using the http2.createSecureServer() method:】
import { createSecureServer } from 'node:http2';
const options = getSecureOptionsSomehow();
options.origins = ['https://example.com', 'https://example.org'];
const server = createSecureServer(options);
server.on('stream', (stream) => {
stream.respond();
stream.end('ok');
});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');
});