OSNOVA
bI?
Modular Node.js server based on express and mongoose.
Install
npm:
npm i osnova [--save]
or yarn:
yarn add osnova
Usage
const OSNOVA = ;const osnova = ; osnovastart { console;};
Modules
OSNOVA is modular by its nature. Modules are just functions, that will be called on start.
Every module added to OSNOVA will be called with osnova
as an argument.
The very basic module:
const myCounter = { let count = initialCount; osnovanext counter: { return count; } { count++; } { count--; } }
osnova.next({ export })
assigns export
object to osnova
. So myCounter
will be available
as osnova.counter
for other modules.
Important moment here - next()
is not just making available some functionality from outside.
It is also an indicator that module has done his business and OSNOVA should execute next one in the queue.
Every module MUST CALL osnova.next()
even if it is not exporting anything.
By this mechanism modules will guaranteed to be executed in an
order they were added and any next module
gain access to exports of all previous modules:
const osnova = ;
osnova.start
takes the module as an argument. It will be added in queue as the last one.
And then module queue will be executed. So module added by start
can safely use any other module.
osnovastart { const counter = osnovacounter; counter; console;};
Core modules
These modules are built-in and will be called by default in the next order:
Mongo
Exports mongo
object:
mongo: connection // current connection to MongoDB
Webserver
Exports express application as express
and http-server as http
.
Takes Options:
- compression { boolean } if on - express will use compression middleware as the first one.
default: true
- midllewares { array | object } standard express middlewares list.
default: {}
Session
Exports session
object:
session: store key secret
Takes options:
- store: session store.
default:
provided byconnect-mongo
andexpress-session
- key: session key.
default: osnova.opts.core.session.key
- secret: session secret string.
default: osnova.opts.core.session.secret
- connection: connection to MongoDB.
default: osnova.mongo.connection
Core modules can be configured in OSNOVA options object core.modules
branch:
const osnova =
OSNOVA Options
-
listen { function } will be called with
osnova.http
as an argument when all modules are ready. ifundefined
of'default'
will be used built-in standard listen. -
DEBUG { object } enable debug-messages mode for specific parts of OSNOVA.
default:
everything is off.
Samples
Add routes
You can't do this:
const osnova = ; const express = osnovaexpress; // app - undefinedexpress; osnovastart;
The right way is to call routing from start
callback or create a module:
const myRouting = { const express = osnovaexpress; express; express; osnovanext;} const osnova = ; osnovastart;