npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@appolo/engine

v8.0.26

Published

nodejs server framework

Downloads

803

Readme

Appolo-Engine

Build Status Dependencies status NPM version npm Downloads Known Vulnerabilities

appolo

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.

Features

  • Super fast
  • MVC Architecture
  • Dependency injection
  • Routes validation
  • Modules system
  • Manage easily configurations and environments
  • Simple folder structures
  • Easy to get started

Installation

npm install @appolo/engine --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/engine';
createApp().launch();

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
        |- all.ts
        |- development.ts
        |- production.ts

	|- modules
	    |- all.ts
|- src
    |- controllers
    |- managers
    |- services
    |- bootstrap.ts
    |- app.ts

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']| | root | the root folder of the paths option | string | process.cwd() | | environment | environment file name that will override the settings in environments/all.js | string | (process.env.NODE_ENV || 'development') |

usage example:

import {createApp}  from '@appolo/engine';
(async ()=>{
    let app = await createApp({
        paths:[ 'src'],
        root : process.cwd()+'/app',
        environment : 'testing'
     }).launch();
 })();

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
export = {
  name:'all',
  someVar:'someVar'
}
//development.ts
export = {
  name:'develpment',
  db:'mongo://development-url'
}
//development.ts
export = {
  name:'testing',
  db:'mongo://testing-url'
}

If we launch our app.js with NODE_ENV = testing

import {createApp}  from '@appolo/engine';
...
let app = await createApp().launch();
var env = appolo.env;
console.log(env.name,env.someVar,env.db) // 'testing someVar monog:://testing-url'

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 injectable
  • singleton - the class will be created only once and the injector will return the same instance every time
  • lazy - wait for the class to be injected before creating it
  • alias - add alias name to the object (allows injecting multiple objects which share an alias using injectAlias)
  • aliasFactory - add alias factory name to the object (allows injecting multiple objects which share an alias using injectAliasFactory)

methods decorators

  • initMethod - The method will be called after all instances were created and all the properties injected.

property decorators

method parameter decorators

  • injectParam - inject object by parameter
//dataRemoteManager.ts
import {define,singleton,initMethod,inject,IFactory,factory} from '@appolo/inject';
@define()
@singleton()
export class DataRemoteManager {
    getData(){ ...}
}
//dataManager.ts
@define()
@singleton()
@factory()
export class DataManager implement IFactory {
    @inject() dataRemoteManager:DataRemoteManager

    get(){
        return this.dataRemoteManager;
    }
}
//fooController.ts
@controller()
export class FooController{
    @inject() dataManager:DataManager
    constructor() {
        this.data = null
    }

    @initMethod()
    initialize(){
        this.data =  this.dataManager.getData();
    }

    @get("/data")
    getData(){
        return this.data;
    }
}

You can also use constructor injection or method parameter injection:

import {define,singleton,injectParam,initMethod,inject} from '@appolo/inject';
@define()
@singleton()
export class DataManager {
    getData(){ ... }
}
@define()
class FooController{
    constructor(@injectParam() dataManager:DataManager) {
        this.dataManager = dataManager;
    }

    @initMethod()
    public initialize(){
        this.data =  this.dataManager.getData();
    }

    public test(@injectParam() logger:Logger){... }
}

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.

import {define,singleton,injectParam,initMethod,inject} from '@appolo/inject';

export class BaseManager {
    @inject() protected env:any
    private getData(){...}
}

@define()
class FooManager extends BaseManager{
    @initMethod()
    public initialize(){
        //the env object in injected from the base class
        console.log(this.env.test)
    }
}

Event Dispatcher

Appolo has a built-in event dispatcher to enable classes to listen to and fire events. Event Dispatcher has the following methods:

import {define,singleton,injectParam,initMethod,inject} from '@appolo/inject';
import {EventDispatcher} from '@appolo/events';
@define()
@singleton()
export class FooManager extends EventDispatcher{
    public notifyUsers(){
        this.fireEvent('someEventName',{someData:'someData'})
    }
}
@define()
export class FooController {
    @inject() fooManager:FooManager;
    @initMethod()
    public initialize(){
        this.fooManager.on('someEventName',(data)=>{
            this.doSomething(data.someData)
        },this);
    }
    doSomething(data){...}
}

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:

import {App} from '@appolo/engine';
import {Injector} from '@appolo/inject';
export = async function(app:App){
    await app.module(async function(env:any,inject:Injector){
        let myModuleObject = {data:'test'};
        await toSomeThing();
        inject.addObject('myModuleObject',myModuleObject);
    });
}

Now we can inject myModuleObject to any class:

import {define,singleton,initMethod,inject} from '@appolo/inject';
@define()
export  class AuthMiddleware{
    @inject('myModuleObject') testObject:any
    public doSomeThing() {
        return this.testObject.data; //return 'test'
    }
}

Logger module example

A logger module example with winston

loggerModule.js file:

import winston = require('winston');
import {App} from '@appolo/engine';
import {Injector} from '@appolo/inject';
export = async function(app:App){
    await appolo.module(async function(env:any,inject:Injector){
        transports = [];
        transports.push(new (winston.transports.Console)({
            json: false,
            timestamp: true
        })
    });
    let logger = new (winston.Logger)({  transports: transports});
    inject.addObject('logger', logger);});

Now we you inject logger anywhere we need it:

import {define,singleton,initMethod,inject} from '@appolo/engine';
@define()
export class DataManager{
    @inject() logger:Logger
    public initialize(){
        this.logger.info("dataManager initialized",{someData:'someData'})
    }
}

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

import {bootstrap,IBootstrap} from '@appolo/engine';
import {define,singleton,injectParam,initMethod,inject} from '@appolo/inject';
@define()
@bootstrap()
export class Bootstrap implements IBootstrap{
    @inject() someManager1:SomeManager1
    public async run(){
        //start your application logic here
        await this.someManager1.doSomeThing();
    }
}

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.