GithubHelp home page GithubHelp logo

ijse / koa-load-routes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gbahamondezc/koa-load-routes

0.0 2.0 0.0 19 KB

Koa@next middleware to load routes from files and directories using koa-router@next module

License: MIT License

JavaScript 100.00%

koa-load-routes's Introduction

koa-load-routes

NPM version Build Status Dependency Status Coverage Status

koa@next middleware to load routes from files and directories using koa-router@next module

Warning

Node.js 4.0 and koa@next or latest are requireds to use this module.

Installation

$ npm install --save koa-load-routes

Usage

Options

path firstMiddleware base args

  • String path

  • Path from where the module will try to load the routes, if is a file, it will read the routes defined just inside of the file, otherwise, if is a directory, will read all .js files in the directory and load the routes inside each one.

  • Function|GeneratorFunction firstMiddleware

    • Middleware will be attached to each route loaded, can be a generator function or a normal function, this middleware will be attached previous to each route, useful for authentication middleware.
  • String base

    • Adds the "base" string at the start of each loaded route url.
  • Array args

    • Arguments to be pased inside the route main function, each array elements are passed as a independent argument (see examples below).

Loading sync from file or directory

app.js

'use strict';

const Koa = require('koa');
const loader = require('koa-load-routes');

var app = new Koa();

/* If path is a directory, loader will read files inside
 * to load routes in each file.
 */

app.use(loader({
  path  : '/routes.js',
  args  : [200, {name : 'somename'}],
  // base : 'api'
}));

app.listen(3000, function() {
  console.log('server started at http://127.0.0.1:3000/');
});

routes.js (support generator functions even on [email protected])

'use strict';

module.exports = function ($status, $body) {

  this.get('/', function *(next) {
    yield next;
    this.status = $status;
    this.body = $body;
  })

  this.get('/hello', function (ctx, next) {
    ctx.body = 'Hello world';
    return next();
  });
  
  // Routes chain
  this.get('/hello2', function(ctx, next) {
    ctx.body = "hello world 2";
  })
  .get('/hello3', function *(next) {
    this.body = "hello world 3";
  });
  
  // Multiple middlewares
  this.get('/multiple', function(ctx, next) {
    console.log('im the first one');
    return next();
  },
  function *(next) {
	this.body = "yey!";
  });

};

License

MIT © Gonzalo Bahamondez

koa-load-routes's People

Contributors

gbahamondezc avatar ijse avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.