UNPKG

@antibot/server

Version:

## 🖥️💫 A quick express backend module.

89 lines 3.89 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _Server_instances, _Server_middleware, _Server_mount; Object.defineProperty(exports, "__esModule", { value: true }); exports.Server = void 0; const express_1 = __importDefault(require("express")); const helmet_1 = __importDefault(require("helmet")); const cors_1 = __importDefault(require("cors")); const express_rate_limit_1 = __importDefault(require("express-rate-limit")); const glob_1 = __importDefault(require("glob")); const path_1 = __importDefault(require("path")); class Server { constructor(options) { _Server_instances.add(this); this.options = options; this.app = (0, express_1.default)(); this.options = options; __classPrivateFieldGet(this, _Server_instances, "m", _Server_middleware).call(this); __classPrivateFieldGet(this, _Server_instances, "m", _Server_mount).call(this); } start() { if (this.options.settings.debug) { this.app.listen(this.options.port, () => { console.log(`Now listening to port ${this.options.port}`); }); } else { this.app.listen(this.options.port); } } get getConfig() { return this.options; } } exports.Server = Server; _Server_instances = new WeakSet(), _Server_middleware = function _Server_middleware() { if (this.options.settings) { if (this.options.settings.useJson) { this.app.use(express_1.default.json(this.options.settings.json)); } if (this.options.settings.useText) { this.app.use(express_1.default.text(this.options.settings.text)); } if (this.options.settings.useUrlencoded) { this.app.use(express_1.default.urlencoded(this.options.settings.urlencoded)); } } if (this.options.cors) { this.app.use((0, cors_1.default)(this.options.cors)); } if (this.options.helmet) { this.app.use((0, helmet_1.default)(this.options.helmet)); } if (this.options.ratelimit) { this.app.use((0, express_rate_limit_1.default)(this.options.ratelimit)); } if (this.options.settings.viewEngine) { this.app.set("view engine", this.options.settings.viewEngine); } }, _Server_mount = function _Server_mount() { if (this.options.settings.routesDirectory) { const routes = glob_1.default.sync(path_1.default.join(this.options.settings.routesDirectory, "**/**/*.js")); if (this.options.settings.debug) { console.log("Found routes:", routes); } routes.forEach((routePath) => { const routeModule = require(path_1.default.resolve(routePath)); if (this.options.settings.debug) { console.log("Loaded route:", routePath); } this.app.use(this.options.settings.routesEndpoint, routeModule.default); }); } if (this.options.settings.views) { const views = glob_1.default.sync(path_1.default.join(this.options.settings.views, `**/**/*.${this.options.settings.viewExt}`)); if (this.options.settings.debug) { console.log("Found views:", views); } this.app.set("views", this.options.settings.views); } }; //# sourceMappingURL=Server.js.map