UNPKG

@nodeboot/http-server

Version:

Node-Boot http server package. It provides a simple way to create HTTP servers using Node.js, with support for routing, middleware, and request handling.

90 lines 3.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpServer = void 0; const context_1 = require("@nodeboot/context"); const core_1 = require("@nodeboot/core"); const engine_1 = require("@nodeboot/engine"); const node_http_1 = require("node:http"); const driver_1 = require("../driver"); const find_my_way_1 = __importDefault(require("find-my-way")); class HttpServer extends core_1.BaseServer { server; router = (0, find_my_way_1.default)(); constructor() { super("native-http"); this.server = (0, node_http_1.createServer)(); } async run(additionalConfig) { const context = context_1.ApplicationContext.get(); await super.configure(this.server, this.router, additionalConfig); if (context.applicationAdapter) { const engineOptions = context.applicationAdapter.bind(context.diOptions?.iocContainer); const serverConfigs = this.getServerConfigurations(); if (!serverConfigs) { this.logger.warn(`No Server configurations provided for HTTP Server . To enable server configurations for CORS, Session, Multipart, Cookie and Templating, consider creating a @Bean(SERVER_CONFIGURATIONS) that returns an "HttpServerConfigs" object`); } const driver = new driver_1.HttpDriver({ logger: this.logger, server: this.server, router: this.router, serverConfigs: serverConfigs, }); engine_1.NodeBootToolkit.createServer(driver, engineOptions); } else { throw new Error("Error stating Application. Please enable NodeBoot application using @NodeBootApplication"); } return this; } async listen() { return new Promise((resolve, reject) => { const context = context_1.ApplicationContext.get(); const port = context.applicationOptions.port || 3000; this.server.listen(port, "0.0.0.0", () => { this.logger.info(`=================================`); this.logger.info(`======= ENV: ${context.applicationOptions.environment} =======`); this.logger.info(`🚀 App listening on ${this.server.address()}`); this.logger.info(`=================================`); // mark the server as started super.started(); // Server initialized resolve(this.appView()); }); this.server.on("error", err => { this.logger.error("Failed to start server", err); reject(err); process.exit(1); }); }); } async close() { this.server.close(err => { if (err) { this.logger.error("NodeBoot HTTP Server closed with error", err); } else { this.logger.info("NodeBoot HTTP Server closed successfully"); super.stopped(); } }); } async configureHttpLogging() { // If you want to add any HTTP logging hooks or specific logging here, do it. this.logger.info("Configuring HTTP logging for HttpServer."); // The actual request logging is handled by the HttpDriver. } getHttpServer() { return this.server; } getFramework() { return this.server; } getRouter() { return this.router; } } exports.HttpServer = HttpServer; //# sourceMappingURL=HttpServer.js.map