mirror of
https://github.com/sergeyyarkov/asu_tools_service.git
synced 2026-10-11 02:19:09 +03:00
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
import http from "node:http";
|
|
import { routeHandler } from "./router/index.js";
|
|
import packageJson from "./package.json" with { type: "json" };
|
|
import { ctxStorage } from "./context.js";
|
|
import db from "./db.js";
|
|
import { createResponse } from "./response.js";
|
|
import { createRequest } from "./request.js";
|
|
|
|
const SERVER_PORT = 3000;
|
|
|
|
function application() {
|
|
const server = http.createServer((req, res) => {
|
|
try {
|
|
const url = new URL(`http://localhost:${SERVER_PORT}${req.url}`);
|
|
|
|
/** @type {HTTPContext} */
|
|
const ctx = {
|
|
data: null,
|
|
req: createRequest(req),
|
|
res: createResponse(res)
|
|
};
|
|
|
|
ctxStorage.run(ctx, () =>
|
|
routeHandler(url).then(() => {
|
|
console.log(`${new Date().toISOString()} Request: ${url.pathname}`);
|
|
})
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
});
|
|
|
|
return {
|
|
/**
|
|
* @param {number} port
|
|
*/
|
|
async run(port = 3000) {
|
|
await db.connect().then(() => console.log("Database connected!"));
|
|
server.listen(port, () => {
|
|
console.log(`Service ${packageJson.name} is running at port ${SERVER_PORT}.`);
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
application().run(SERVER_PORT);
|