所有内部模块(包括http2)都是在Node启动时注册的。引用source:
// A list of built-in modules. In order to do module registration
// in node::Init(), need to add built-in modules in the following list.
// Then in binding::RegisterBuiltinModules(), it calls modules' registration
// function. This helps the built-in modules are loaded properly when
// node is built as static library. No need to depend on the
// __attribute__((constructor)) like mechanism in GCC.
#define NODE_BUILTIN_STANDARD_MODULES(V)
所以是的,我宁愿在你的情况下使用那个模块。
顺便说一下,从技术上讲,HTTP 状态码甚至不是 http2 模块的一部分:它们作为一组宏存储在 node_http_common.h 文件中:
#define HTTP_STATUS_CODES(V) \
V(CONTINUE, 100) \
V(SWITCHING_PROTOCOLS, 101) \
V(PROCESSING, 102) \
V(EARLY_HINTS, 103) \
V(OK, 200) \
// ...
...最后,在另一系列#define 语句中连接到http2 module constants 道具:
#define V(name, _) NODE_DEFINE_CONSTANT(constants, HTTP_STATUS_##name);
HTTP_STATUS_CODES(V)
#undef V
有趣的是,Node 中还有另一个地方存储了相同的数据(状态码是键,而不是值):它是 http module。 Source:
const STATUS_CODES = {
100: 'Continue', // RFC 7231 6.2.1
101: 'Switching Protocols', // RFC 7231 6.2.2
102: 'Processing', // RFC 2518 10.1 (obsoleted by RFC 4918)
103: 'Early Hints', // RFC 8297 2
200: 'OK', // RFC 7231 6.3.1
201: 'Created', // RFC 7231 6.3.2
// ...