我有一个带有 MySQL 数据库的 Node 应用程序。我想将每个 HTTP 请求关联或映射到 MySQL 数据库调用。 即要求是以某种方式将 HTTP 请求标识传递给随后的异步数据库调用(下一个异步调用完全在一个新的上下文中)。
请给我一些建议。
最佳答案
经过大量研究,我发现在 node.js 中,async_hooks 模块提供了一个 API 来注册回调,跟踪在 Node.js 应用程序中创建的异步资源的生命周期。
const async_hooks = require('async_hooks');
const eid = async_hooks.executionAsyncId(); // Return the ID of the current execution context.
const tid = async_hooks.triggerAsyncId(); // Return the ID of the handle responsible for triggering the callback of the current execution scope to call.
关于javascript - 如何将 HTTP 请求映射或关联到 Node.js 中的数据库调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869687/