This commit is contained in:
Sergey Yarkov 2026-03-01 21:28:50 +03:00
parent db6de0b2f5
commit 8c5d52926d
4 changed files with 9 additions and 7 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
uploads/*
.env
.vscode

View File

@ -1,3 +1,3 @@
import { AsyncLocalStorage } from "node:async_hooks";
export const ctxAsyncLocalStorage = new AsyncLocalStorage();
export const httpStorage = new AsyncLocalStorage();

View File

@ -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}`);
})

View File

@ -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(