node-fast-server
Version:
It enables you to effortlessly create a high-performance API server built with Node.js and Express.
120 lines (119 loc) • 4.68 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FastServer = void 0;
var express_1 = __importDefault(require("express"));
var http_1 = __importDefault(require("http"));
var types_1 = require("../types");
var cookie_parser_1 = __importDefault(require("cookie-parser"));
var cors_1 = __importDefault(require("cors"));
var FastServer = /** @class */ (function () {
/**
*
* @param apis
* An array consisting of JSON objects representing ApiTypes[].
*
* @example
* [[ {method,name,endpoint,middleware?,contorller}] ]
*
*
* const apis:ApiListType[]=[
* [{method:"POST",name:"cutomApi",endpoint:"cutomApi",contorller:cutomApi.controller},
* [{method:"GET",name:"cutomApi1",endpoint:"cutomApi1",contorller:cutomApi.controller1},
*
* ],
* [{method:"GET",name:"cutomApi3",endpoint:"cutomApi3",middleware:customApi3.middleware,contorller:cutomApi3.controller}]
*
* ]
*/
function FastServer(apis, config) {
if (config === void 0) { config = {
SERVER_PORT: 4444,
WS_PORT: 4445,
SERVER_TYPE: types_1.serverType.Develop,
PUBLİC_FOLDER_NAME: "FILES",
PREFIX: "api/v1"
}; }
this.APP = (0, express_1.default)();
this.EXPRESS = express_1.default;
this.HTTP = http_1.default;
// this.STARTSERVER();
this.apis = apis;
this.config = config;
}
FastServer.prototype.STARTSERVER = function () {
var _a;
this.USE();
this.ACTIVE_CORS();
this.ACTIVE_SHARE_FOLDERS(this.config.PUBLİC_FOLDER_NAME);
this.LISTEN_API((_a = this.apis) !== null && _a !== void 0 ? _a : [[{ name: "test", method: "GET", controller: function () { }, endpoint: "test" }]]);
this.ACTIVE_WS();
this.LISTEN();
};
FastServer.prototype.USE = function () {
var ex = this.EXPRESS;
var app = this.APP;
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(ex.json());
app.use(ex.urlencoded());
app.use((0, cookie_parser_1.default)());
};
FastServer.prototype.LISTEN = function () {
var _this = this;
var app = this.APP;
var port = this.config.SERVER_PORT;
var host = this.config.HOST || "0.0.0.0";
var server = app.listen({ port: port, hostname: host }, function () {
console.log("success", "server is running in ".concat(_this.config.SERVER_TYPE, " mode and on port ").concat(port, " "));
});
return server;
};
FastServer.prototype.ACTIVE_CORS = function () {
var app = this.APP;
app.use((0, cors_1.default)());
};
FastServer.prototype.ACTIVE_WS = function () {
var createServer = this.HTTP.createServer;
var app = this.APP;
var port = this.config.WS_PORT;
var host = this.config.HOST || "0.0.0.0";
var ws_server = createServer(app);
ws_server.listen({ port: port, hostname: host }, function () {
var address = ws_server.address();
console.log("info", "WS Server is running on port ".concat(JSON.stringify(address)));
});
return ws_server;
};
FastServer.prototype.ACTIVE_SHARE_FOLDERS = function (foldername) {
var app = this.APP;
var ex = this.EXPRESS;
app.use(ex.static(foldername));
};
FastServer.prototype.LISTEN_API = function (apis) {
var _this = this;
var app = this.APP;
apis.forEach(function (t) {
t.forEach(function (_t) {
var _a;
(_a = app)[_t.method.toLowerCase()].apply(_a, __spreadArray(__spreadArray(["/".concat(_this.config.PREFIX, "/").concat(_t.endpoint)], (_t.middleware ? [_t.middleware] : []), false), [_t.controller], false));
});
});
};
return FastServer;
}());
exports.FastServer = FastServer;