message.url


仅对从 http.Server 获取的请求有效。

请求 URL 字符串。它仅包含实际 HTTP 请求中存在的 URL。请看以下请求:

【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.】