diff --git a/.gitignore b/.gitignore index 54e0550..c3d4a43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules uploads/* -.env \ No newline at end of file +.env +.vscode \ No newline at end of file diff --git a/context.js b/context.js index a827c25..fe44f85 100644 --- a/context.js +++ b/context.js @@ -1,3 +1,3 @@ import { AsyncLocalStorage } from "node:async_hooks"; -export const ctxAsyncLocalStorage = new AsyncLocalStorage(); +export const httpStorage = new AsyncLocalStorage(); diff --git a/index.js b/index.js index 790015b..ca5bd00 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ import http from "node:http"; import { routeHandler } from "./router/index.js"; import packageJson from "./package.json" with { type: "json" }; -import { ctxAsyncLocalStorage } from "./context.js"; +import { httpStorage } from "./context.js"; // import db from "./db.js"; import response from "./response.js"; import request from "./request.js"; @@ -21,10 +21,11 @@ http req: Object.create(request), res: Object.create(response) }; + ctx.req.req = req; ctx.res.res = res; - ctxAsyncLocalStorage.run(ctx, () => + httpStorage.run(ctx, () => routeHandler(url).then(() => { console.log(`${new Date().toISOString()} Request: ${url.pathname}`); }) diff --git a/router/index.js b/router/index.js index b01c885..29590aa 100644 --- a/router/index.js +++ b/router/index.js @@ -1,6 +1,6 @@ import path from "node:path"; import formidable from "formidable"; -import { ctxAsyncLocalStorage } from "#root/context.js"; +import { httpStorage } from "#root/context.js"; import { handlers as reportsHandlers } from "./reports/index.js"; /** @@ -30,7 +30,7 @@ export const ROUTE_MAP = { */ export async function routeHandler(url) { /** @type {HTTPContext} */ - const ctx = ctxAsyncLocalStorage.getStore(); + const ctx = httpStorage.getStore(); const { req, res } = ctx; try { @@ -53,7 +53,7 @@ export async function routeHandler(url) { return; } - res.sendJson({ error: { message: "Указанный URL не имеет обработчика." } }, 404); + res.sendJson({ error: { message: "Route not found." } }, 404); } catch (error) { console.error(error); res.sendJson(