如何将 HTTP 请求映射或关联到 Node.js 中的数据库调用?

How to map or associate HTTP request to db call in Node.js?

我有一个带有 MySQL 数据库的节点应用程序。我想将每个 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.