message.url
仅对从 http.Server 获得的请求有效。
¥Only valid for request obtained from http.Server.
请求的网址字符串。这仅包含实际 HTTP 请求中存在的网址。接受以下请求:
¥Request URL string. This contains only the URL that is present in the actual HTTP request. Take the following request:
GET /status?name=ryan HTTP/1.1
Accept: text/plain 要将网址解析为它的部分:
¥To parse the URL into its parts:
new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`); 当 request.url 为 '/status?name=ryan' 且 process.env.HOST 未定义时:
¥When request.url is '/status?name=ryan' and process.env.HOST is undefined:
$ node
> new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`);
URL {
href: 'http://localhost/status?name=ryan',
origin: 'http://localhost',
protocol: 'http:',
username: '',
password: '',
host: 'localhost',
hostname: 'localhost',
port: '',
pathname: '/status',
search: '?name=ryan',
searchParams: URLSearchParams { 'name' => 'ryan' },
hash: ''
} 确保将 process.env.HOST 设置为服务器的主机名,或考虑完全替换这部分。如果使用 req.headers.host,请确保使用正确的验证,因为客户端可能会指定自定义 Host 标头。
¥Ensure that you set process.env.HOST to the server's host name, or consider
replacing this part entirely. If using req.headers.host, ensure proper
validation is used, as clients may specify a custom Host header.