Appolo is an light web server MVC Framework for Node.js written in Typescript
Appolo architecture follows common patten of MVC and dependency injection which makes it easy to build better performance, flexibility and easy maintenance server side in nodejs.
Getting started
- Visit our docs site appolojs.com
Features
- Super fast
- MVC Architecture
- Full support for express middleware
- Dependency injection
- Simple routing
- Modules system
- Manage easily configurations and environments
- Simple folder structures
- Easy integrate third party modules
- Easy to get started
Installation
npm install appolo --save
Typescript
appolo
requires TypeScript compiler version > 2.1 and the following settings in tsconfig.json
:
"experimentalDecorators": true
Quick Start
In your app.js file:
var createApp from 'appolo';;
Benchmarks
node benchmarks/benchmarks
Machine: 2.2 GHz Intel Core i7, 16GiB RAM
Method: autocannon -c 100 -d 10 -p 10 localhost:3000
Name | Average | Min | Max |
---|---|---|---|
Req/Sec | 32,821.6 | 23,840 | 34,463 |
Examples
Directory Structure
Appolo will require all files in the config
and src
folders, but the env
folder will be loaded first. All other folders are optional
|- config |- env |- allts |- developmentts |- productionts |- middlewares |- allts |- modules |- loggerts |- src |- controllers |- managers |- services |- bootstrapts |- appts
Configuration
appolo launch configuration options, all options are optional
key | Description | Type | Default |
---|---|---|---|
paths |
folders that will be required and loaded on appolo launch | array |
[ 'src'] |
environment |
environment file name that will override the settings in environments/all.js |
string |
(process.env.NODE_ENV || 'development') |
startMessage |
the message that will be written to console log the the server starts | string |
'Appolo Server listening on port: {port} version:{version} environment: {environment}' |
startServer |
if true the server will start immediately to listen to port else you will have to start in manually. | boolean |
true |
port |
the port that the app will listen to. | number |
`process.env.PORT || this._options.port || appolo.environment.port || 8080)`` |
errorMessage |
print route http error.toString() | boolen |
true |
maxRouteCache |
the max size of route lookup lru cache | number |
10000 |
usage example:
;async { let app = await ; };
Environments
With environments you can define different configurations depending on the environment type your app is currently running.
It is recommended to have 4 types of environments: development
, testing
, staging
, production
.
After appolo.launch
you can always access the current environment vars via appolo.environment
.
//all.ts //development.ts //development.ts
If we launch our app.js with NODE_ENV = testing
;...let app = await ; var env = appoloenv; console // 'testing someVar monog:://testing-url'
Express Middlewares
You can configure express modules or add custom middleware by adding configuration files to the express folder.
The express configuration file is called after the environment files were loaded.
//middlewares/all.ts ; ; ; { app; app; app;}
Routes
You can easily bind a route path to a controller method.
The routes path are defined in the same way as in expressjs router.
Each route class has the following methods:
path
- same as in expressjs.method
- one ofget
,post
,patch
,delete
,put
. defaultget
.action
- the action function the will be invoked to handle the route.middleware
- middleware function the will be invoked before the controller. If thenext
function is not called or called with an error, the controller won`t be created.
; @ @ dataManager:DataManager @ public { return thisdataManager); }
or you can return response by using res.send
@ @ dataManager:DataManager @ public { res; }
You can also define routes using appolo.route
method:
; @ @ dataManager:DataManager public { res; } approute<TestController>TestController methodappoloMethodsGET actionctest
Controllers
Controllers are classes that handle routes request.
In order for the router to be able to handle the request, a controller class must extend Controller
.
Each controller action will be called with request and response objects.
; @ @ authManager:AuthManager; @ public async return await thisauthManager
By default, appolo creates a new controller instance for every request. If you do not need a new controller instance for every request, you can inherit from StaticController which is singleton.
; @ @ @ @ authManager:AuthManager; @ public aynsc return await thisauthManager
Custom errors
by default the response will be wrapped with try catch and InternalServerError
will be send to response.
status: 500 message: "Bad Request" error: "Internal Server Error"
or you can throw custom error
; @ @ authManager:AuthManager; @ public async try return await thisauthManager catche throw 401"Not Found"ekey:"value"1000
"status": 401 "message": "Not Foundr" "error":"something is wrong" "code":1001key:"value"
Middleware
A middleware class will run before the action of the controller is invoked.
The middleware class must extend must extend appolo.Middleware
and implement the run
method.
Middleware file:
; @ @ authManager:AuthManager; public async { try let user = await thisauthManager requser = user; ; catche this; }
now you can added the middleware to our route
@ @ @ public async { return requser }
Dependency Injection System
Appolo has a powerful Dependency Injection system based on appolo-inject.
It enables you to write organised, testable code based on the loose coupling idea.
You can always access the injector via app.injector
.
class decorators
define
- make the object injectablesingleton
- the class will be created only once and the injector will return the same instance every timelazy
- wait for the class to be injected before creating italias
- add alias name to the object (allows injecting multiple objects which share an alias usinginjectAlias
)aliasFactory
- add alias factory name to the object (allows injecting multiple objects which share an alias usinginjectAliasFactory
)
methods decorators
initMethod
- The method will be called after all instances were created and all the properties injected.
### property decoratorsinject
- inject instance reference by idinjectFactoryMethod
- factory method is a function that will return the injected object. This is useful to create many instances of the same class.injectAlias
- inject objects by alias nameinjectArray
- inject array of properties by reference or by valueinjectDictionary
- inject a dictionary of properties by reference or by value.injectAliasFactory
- inject factory methods by alias nameinjectFactory
inject object by factory classinjectObjectProperty
inject property of another objectinjectValue
inject property by value
method parameter decorators
injectParam
- inject object by parameter
//dataRemoteManager.ts ; @ @ { ...}//dataManager.ts @ @@ implement IFactory @ dataRemoteManager:DataRemoteManager { return thisdataRemoteManager; } //fooController.ts @ @ dataManager:DataManager { thisdata = null } @ { thisdata = thisdataManager; } @ { return thisdata; }
You can also use constructor injection or method parameter injection:
; @ @ { ... } @ { thisdataManager = dataManager; } @ public { thisdata = thisdataManager; } public ...
Inherited injections
Inherited injections are supported as well.
Anything you inject on a base class will be available to child classes.
Remember not to use @define
on the parent class.
; @ protected env:any private {...} @ @ public { //the env object in injected from the base class console }
Event Dispatcher
Appolo has a built-in event dispatcher to enable classes to listen to and fire events.
Event Dispatcher has the following methods:
; @ @ public { this } @ @ fooManager:FooManager; @ public { thisfooManager; } {...}
Modules
Third party modules can be easily loaded intto appolo inject and used in the inject container.
Each module must call appolo.module
before it can be used by appolo launcher
.
appolo.module
accepts a function as an argument. The last argument to that function must be the next
function: modules are loaded serially, so each module must call the next
function or return a promise
in order to continue the launch process.
Other arguments to the function are object which you wish to inject into the module (these objects must be injected earlier).
By default, each module can inject:
env
- environment object,inject
- injector - to add objects to the injector,
Module example:
; { await app; }
Now we can inject myModuleObject
to any class:
; @ @ testObject:any public { return thistestObjectdata; //return 'test' }
Logger module example
A logger module example with winston
loggerModule.js file:
; ; { await appolo;
Now we you inject logger anywhere we need it:
; @ @ logger:Logger public { thislogger }
Bootstrap
Once it launched, appolo will try to find an appolo bootstrap
class and call it's run
method. Only when the bootstrap is finished, the server will start
; @ @ implements IBootstrap @ someManager1:SomeManager1 public async { //start your application logic here await thissomeManager1; }
Reset
You can reset appolo sever by calling appolo.reset()
. This will clean all environments, config, injector and close the server.
Tests
grunt test
License
The appolo
library is released under the MIT license. So feel free to modify and distribute it as you wish.