From 4f9a1b8fa2c441268993f64a7be03239d593acf9 Mon Sep 17 00:00:00 2001 From: FullGreaM Date: Wed, 5 Oct 2022 09:27:14 +0300 Subject: [PATCH] upload files into repos --- codeExample.js | 7 +++---- components/Main.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/codeExample.js b/codeExample.js index 3704c77..cd070cf 100644 --- a/codeExample.js +++ b/codeExample.js @@ -1,4 +1,4 @@ -const backend = require('index'); +const backend = require('./index'); // Создаём экземпляр класса backend.Main var server = new backend.Main( @@ -37,7 +37,7 @@ server.paramsError = (required, additional) => ({ required, additional }); // Создаём класс группы методов class ExampleMethodGroup extends backend.Group { - handler (params, session) { // Путевая обработка + handler (params, session) { // Путевая обработка session._setValue('example', 1); // Задать значение console.log(session.example); // Получить значение из сессии session._remove('example'); // Убрать значение @@ -67,7 +67,6 @@ var exampleMethod = new ExampleMethod('/example', { max_length : 255, // allow_methods : ['post'], // allow_params : ['json'], - } }); // Привяжем метод к группе @@ -76,4 +75,4 @@ exampleMethod.group(ExampleMethodGroup); server.method(exampleMethod); // Запускаем сервер -server.server().listen(8080); \ No newline at end of file +server.server('/api/v1'/*, { Информация о SSL }*/).listen(8080); \ No newline at end of file diff --git a/components/Main.js b/components/Main.js index 8758e80..c639c07 100644 --- a/components/Main.js +++ b/components/Main.js @@ -1,6 +1,42 @@ +const express = require('express'); +const https = require('https'); +const version = 'v 1.0.0'; + + class Main { constructor (send_headers = true) { this.send_headers = send_headers; + this.methods = new Object(); + } + + method (methodObj) { + this.methods[methodObj.path] = methodObj; + } + + router (returnMiddlewareFunction = true, middlewareFunction = (req, res, next) => next()) { + let router = express.Router(); + + for (let path in this.methods) { + for (let methodId in this.methods[path].allowedMethods) { + router[this.methods[path].allowedMethods[methodId]](async (req, res) => { + // (!) Mainbody + }); + } + } + + if (returnMiddlewareFunction) { + return middlewareFunction, router; + } + else { + return router; + } + } + + server (mountPath = '/', options = null) { + let app = express(); + + app.use(mountPath, this.router()); + return app; } }