向客户端提交错误响应时,“response.writeHead”的第二个参数是否存在指南?
Do guidelines exist for the second parameter of `response.writeHead` when submitting the errored response to client?
我尝试发送以下消息
Invalid query parameters detected:
=== Error No. 1 ==========
Required property is missing
● Property / element: 'Deserialized query parameters of http://127.0.0.1:90/api/categories route.paginationPageNumber'
This property is 'undefined' while has been marked as required.
● Property / element specification:
{
"type": "number",
"required": true,
"numbersSet": "NATURAL_NUMBER"
}
● Actual value: undefined
● Value before first pre-validation modification: undefined
< etc. >
通过
response. // (is "http.ServerResponse")
writeHead(HTTP_StatusCodes.badRequest, errorMessage).
end();
在 errorMessage
变量中。发生以下 NodeJS 错误:
Caught error:
TypeError [ERR_INVALID_CHAR]: Invalid character in statusMessage
at new NodeError (node:internal/errors:371:5)
at ServerResponse.writeHead (node:_http_server:322:11)
// ...
我不知道 NodeJS 抱怨的是哪个字符(可能是斜线),但我想我不正确地使用了 writeHead
的第二个参数。如果是,有两个问题:
- 哪个
errorMessage
一定是这样的?只是“错误请求”或“未经授权”?
- 我应该在哪里附加上面的登录消息?
可以参考这个
您可以发送这样的回复
let body = error_message; // or whatever response you want to send back to the sender
response
.writeHead(STATUS_CODE, {
'Content-Type': 'your_header_content_type'
}).end(body);
关于状态码,可以参考mozilla convention 这里(使用比较广泛)
我尝试发送以下消息
Invalid query parameters detected:
=== Error No. 1 ==========
Required property is missing
● Property / element: 'Deserialized query parameters of http://127.0.0.1:90/api/categories route.paginationPageNumber'
This property is 'undefined' while has been marked as required.
● Property / element specification:
{
"type": "number",
"required": true,
"numbersSet": "NATURAL_NUMBER"
}
● Actual value: undefined
● Value before first pre-validation modification: undefined
< etc. >
通过
response. // (is "http.ServerResponse")
writeHead(HTTP_StatusCodes.badRequest, errorMessage).
end();
在 errorMessage
变量中。发生以下 NodeJS 错误:
Caught error:
TypeError [ERR_INVALID_CHAR]: Invalid character in statusMessage
at new NodeError (node:internal/errors:371:5)
at ServerResponse.writeHead (node:_http_server:322:11)
// ...
我不知道 NodeJS 抱怨的是哪个字符(可能是斜线),但我想我不正确地使用了 writeHead
的第二个参数。如果是,有两个问题:
- 哪个
errorMessage
一定是这样的?只是“错误请求”或“未经授权”? - 我应该在哪里附加上面的登录消息?
您可以发送这样的回复
let body = error_message; // or whatever response you want to send back to the sender
response
.writeHead(STATUS_CODE, {
'Content-Type': 'your_header_content_type'
}).end(body);
关于状态码,可以参考mozilla convention 这里(使用比较广泛)