1.0.2 • Published 7 years ago

express-core v1.0.2

Weekly downloads
2
License
ISC
Repository
-
Last release
7 years ago

Express-core

Easily scalable application framework on express.js

Легко масштабируемая инфраструктура приложения на express.js

Example / Пример

We need to deploy the application with dozens of middlewares and a bunch of routs. To place all calls app.use /app.METHOD/ .etc in one file is not cool. Therefore, you can scatter middlewares and handlers for routs on different files.

Нам нужно развернуть приложение с десятками middlewares и кучей роутов. Размещать все вызовы app.use/app.METHOD/.etc в одном файле не круто. Поэтому можно раскидать middlewares и handlers для роутов по разным файлам.

const ROOT = __dirname;

const $expressCore = require('express-core');
const $path = require('path');

const handlersPipeline = [
  'main', // main.js
  'other' // other.js
];

const middlewaresPipeline = [
  'cookie-parser' // cookie-parser.js
];

const {app} = new $expressCore({
  handlersPath: $path.resolve(ROOT, 'handlers'),
  middlewaresPath: $path.resolve(ROOT, 'middlewares'),
  handlers: handlersPipeline,
  middlewares: middlewaresPipeline,
  httpLog: true // switcher of logger for http requests
});

app.listen(1337, () => {
  console.log('started listing port');
});

This example is more detailed / Данный пример подробнее

Initialization the ready express app / Инициализация уже созданного express приложения

It may be necessary to initialize the express application before initiating the entire framework. To do this, you can transfer the finished express application in ExpressCore.

Возможно, понадобится инициализировать express приложение до инициадизации всего каркаса. Для этого можно передать готовое express приложение в ExpressCore.

let _app = $express();

...

const {app} = new $expressCore({
  ...
}, _app); // _app set to this.app for expressCore

app.listen(1337, () => {
  console.log('started listing port');
});

Middleware

Middlewares are functions that can manage app during the initialization process themselves.

Middlewares являются функицями, которые могут управлять app в процессе инициализации самостоятельно.

// middlewares/cookie-parser.js

const cookieParser = require('cookie-parser');

// middleware is independent function
function middleware (app) {
  app.use(cookieParser());
}

module.exports = {
  middleware
};

Handler

Handlers are divided into two types:

  • Primitive
  • Independent (similar to middleware)

Handlers делятся на два типа:

  • Примитивные
  • Самостоятельные (анологично middleware)
// Primitive handler

const path = '/path';

// METOD: get/post/put/option/delete/all
const method = 'GET'; 

function handler (req, res, next) {
  return res.end('Hello, world!');
}

module.exports = {
  path,
  method,
  handler
};
// Independent handler

function independent (app) {
  app.all('*', (req, res) => {
    return res.end('Hello, independent!');
  });
}

module.exports = {
  independent
};