mirror of
https://github.com/sergeyyarkov/asu_tools_service.git
synced 2026-10-11 10:29:22 +03:00
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
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 db from "./db.js";
|
|
import response from "./response.js";
|
|
import request from "./request.js";
|
|
|
|
const SERVER_PORT = 3000;
|
|
|
|
// await db.connect().then(() => console.log("Database connected!"));
|
|
|
|
http
|
|
.createServer((req, res) => {
|
|
try {
|
|
const url = new URL(`http://localhost:${SERVER_PORT}${req.url}`);
|
|
|
|
/** @type {HTTPContext} */
|
|
const ctx = {
|
|
data: null,
|
|
req: Object.create(request),
|
|
res: Object.create(response)
|
|
};
|
|
ctx.req.req = req;
|
|
ctx.res.res = res;
|
|
|
|
ctxAsyncLocalStorage.run(ctx, () =>
|
|
routeHandler(url).then(() => {
|
|
console.log(`${new Date().toISOString()} Request: ${url.pathname}`);
|
|
})
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
})
|
|
.listen(SERVER_PORT, () => {
|
|
console.log(`Service ${packageJson.name} is running at port ${SERVER_PORT}.`);
|
|
});
|