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

light-ning

v0.4.6

Published

(ALPHA) framework without dependecies...

Downloads

96

Readme

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: '[email protected]',
      password: 'secret'
    };
        
    let data = this.filtrate(user);
    
    console.log(data); // { login: 'user', email: '[email protected]' }
    
    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

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