light-ning
(ALPHA) framework without dependecies...
Last updated 3 years ago by sentiurin .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install light-ning 
SYNC missed versions from official npm registry.

Light-weight Framework for creating API Services

Light-ning

Light as a feather, fast as lightning...

ES6 only

"To the game, Sherlock!"


import app from 'light-ning';
app.run()

Important!


You need create the "controllers" and "routes" directories where are you calling "run" method.

*create "controllers" directory only if you need to use actions in routes and controllers.

"routes" directory must include "app.js" file and import all your route-files therein:

import './user'
import './articles'

etc.

If your node js file (where are you calling "run" method) not in root directory, you need to set prefix before the "run":

app.set('prefix', 'path to directory');
app.run()

Router:


import { Router } from 'light-ning';

Actions


Router.routes('/user', [
  {
    method: 'GET',
    path: '/',
    action: 'User@index',
  },
  {
    method: 'GET',
    path: '/:id',
    action: 'User@show',
  },
  {
    method: 'POST',
    path: '/',
    action: 'User@create',
  },
  {
    method: 'PUT',
    path: '/:id',
    action: 'User@update',
  },
  {
    method: 'DELETE',
    path: '/:id',
    action: 'User@destroy',
  },
]);

You'll can write method property in any register 'POST' or 'post' etc.

Property "action" it's a path of two parts: ControllerName@Method. (It work only if you use Controllers).

What action do: User@index equals UserController.index(req, res) and pass object with req, res, etc. options in "index" method. More below...

*Method for action cannot be static.

Middlewares


Router.routes('/user', [
  {
    method: 'GET',
    path: '/',
    middleWares: [ firstFunc, secondFunc ],
  },
]);

Middlewares performed in succession from left to right. For pass the course, you need call "next" in your middleware:

const middleware = next => {
    
    if (true) {
        next();
    } else {
        throw new Error('access denied');
    }
        
}

You'll can pass data in your middleware like this:

const middleware_1 = next => {
    if (true) {
        next(anyData);
    }
}
const middleware_2 = (data, next) => {
   console.log(data); // anyData
}

Important!


Do not place "middleWares" with "action" in one route. In this case, "middleWares" will be ignored.

Method name - can be any

Guard


You'll can use guard property for enable "guardian" mode.

Router.routes('/user', [
  {
    method: 'GET',
    path: '/',
    guard: true,
    action: 'user@index'
  },
]);

After enable, you need to set "guardian" middleware into "app":

const guardian = ({req, next, response}) => {
    
    if (req.body.user === session.user) {
        next();
    } else {
        response({ code: 403 });
    }
        
}
    
app.set('guardian', guardian);

guardian middleware get default req, res node js objects, next function for pass the course and custom response method for easy reponses. More below...

Root path


Setting the root path for group routes.

Do not export "Router"

Router.routes('/user', [
  {
    method: 'GET',
    path: '/',
    action: 'User@index',
  },
  {
    method: 'GET',
    path: '/:id',
    action: 'User@show',
  },

This router create routes like:

 /user/
 /user/:id

etc.

Controllers:


After route action: 'User@index', object with data get in controller. It looks like:

 {
    req: request, // default node js object
    res: response, // default node js object
    response: Response, // method for easy response
 }

Important!

Controller class should export. (only export, not default export)

export class UserController {
    
  index(data) {
    let {req, res} = data;
  }
    
  show({req, response}) {
    if (req.body.user === session.user) {
        return response({ data: session.user });
    }
  
    response({ code: 403 });
  }
}

If you not pass custom error into response object, they get default error from http-errors dictionary by status code.

Method "Response" has 3 params:

{
    error: 'Are you ok?', // custom error
    code: 422, // status code
    data: []
}

Important!


  • "error" must sending with "code" (code can sending without error)
  • "data" must sending without "error" (in this case, data will be ignored)

Base Controller

You'll can extend your controller from BaseController:

import { BaseController } from 'light-ning';
export class UserController extends BaseController {
    
  filter = ['password']; // es7
    
  constructor() {
    super();
    this.filter = ['password'] // es6
  }
    
  index({response}) {
    let user = {
      login: 'user',
      email: 'user@mail.com',
      password: 'secret'
    };
        
    let data = this.filtrate(user);
    
    console.log(data); // { login: 'user', email: 'user@mail.com' }
    
    response({ data: data });
  }
    
};

BaseController has method filtrate for easy filter data for response.

filtrate works from filterMode ('omit' by default). You'll can set 2 modes:

  • omit
  • pick

their names speak for themselves.

filter = ['password'];

Omit mode:

filterMode = 'omit';
this.filtrate({
    login: 'user',
    password: 'secret'
});
    
// result: { login: 'user' }

Pick mode:

filterMode = 'pick';
this.filtrate({
  login: 'user', 
  password: 'secret'
});
    
// result: { password: 'secret' }

Configure

<bt>

You'll can create config.json near your node js file (where are you calling "run" method) and set:

{
    "port": your port
}

or you can set port in app:

app.set('port', your port);
app.run();

Default port: 6060

look source on github

Current Tags

  • 0.4.6                                ...           latest (3 years ago)

46 Versions

  • 0.4.6                                ...           3 years ago
  • 0.4.5                                ...           3 years ago
  • 0.4.4                                ...           3 years ago
  • 0.4.3                                ...           3 years ago
  • 0.4.2                                ...           3 years ago
  • 0.4.1                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.9                                ...           3 years ago
  • 0.3.8                                ...           3 years ago
  • 0.3.7                                ...           3 years ago
  • 0.3.6                                ...           3 years ago
  • 0.3.5                                ...           3 years ago
  • 0.3.4                                ...           3 years ago
  • 0.3.3                                ...           3 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.9                                ...           3 years ago
  • 0.2.8                                ...           3 years ago
  • 0.2.7                                ...           3 years ago
  • 0.2.6                                ...           3 years ago
  • 0.2.5                                ...           3 years ago
  • 0.2.4                                ...           3 years ago
  • 0.2.3                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.9                                ...           3 years ago
  • 0.1.8                                ...           3 years ago
  • 0.1.7                                ...           3 years ago
  • 0.1.6                                ...           3 years ago
  • 0.1.5                                ...           3 years ago
  • 0.1.4                                ...           3 years ago
  • 0.1.3                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.0.9                                ...           3 years ago
  • 0.0.8                                ...           3 years ago
  • 0.0.7                                ...           3 years ago
  • 0.0.6                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |