http.validateHeaderName(name)
name<string>
对提供的 name 执行低级验证,这些验证在调用 res.setHeader(name, value) 时进行。
🌐 Performs the low-level validations on the provided name that are done when
res.setHeader(name, value) is called.
将非法值作为 name 传递会导致抛出 TypeError,错误代码为 code: 'ERR_INVALID_HTTP_TOKEN'。
🌐 Passing illegal value as name will result in a TypeError being thrown,
identified by code: 'ERR_INVALID_HTTP_TOKEN'.
在将头信息传递给 HTTP 请求或响应之前,并不需要使用这种方法。HTTP 模块会自动验证这些头信息。示例:
🌐 It is not necessary to use this method before passing headers to an HTTP request or response. The HTTP module will automatically validate such headers. Examples:
示例:
🌐 Example:
const { validateHeaderName } = require('node:http');
try {
validateHeaderName('');
} catch (err) {
err instanceof TypeError; // --> true
err.code; // --> 'ERR_INVALID_HTTP_TOKEN'
err.message; // --> 'Header name must be a valid HTTP token [""]'
}