1.0.13 • Published 7 years ago

koa-folder-routes v1.0.13

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

koa-folder-routes

Create koa-router objects with folder path appended.

Eg:

npm.io

Steps

1. Create your koa-router objects like below:

    controllers/rest/v1/post/index.js

	'use strict';
	var router = require("koa-router")();

	router.get("post","/:id", function *(next) {
	  var id = parseInt(this.params.id);
	  var postsHelper = PostsHelper(this.mongo);
	  this.body = yield postsHelper.getPostById(id);
	});

	router.allowedMethodsObject = {
	  throw: true,
	  notImplemented: () => new Boom.notImplemented(),
	  methodNotAllowed: () => new Boom.methodNotAllowed()
	};

	module.exports = router;
	

Now this will be available at http://localhost:8080/rest/v1/post/10

2. The koa-router objects present under controllers/root/ are put up directly to the context path.

    controllers/root/index.js

'use strict';

var router = require("koa-router")();

router.get("/", function *(next) {
  this.render("index", {foo:"bar"});
});

router.allowedMethodsObject = {};

module.exports = router;

Now the index.jade will be available at http://localhost:8080/

3. Configuration:

    app.js

	'use strict';
	const koa = require('koa');
	const app = koa();
	var routesloader = require('koa-folder-routes');
	routesloader(app);
	

    config/default.json

		"routes": {
    			"directory": "controllers"
    		}
		

     OR

    app.js

	'use strict';
	const koa = require('koa');
	const app = koa();
	var routesloader = require('koa-folder-routes');
	routesloader(app,'controllers'); 
	

The folder path needs to be referenced from the project's directory (excluding project's directory name).

4.  This module completely depends on koa-router and does not offer anything on it's own for routing. This module only helps invoking app.use function by passing your koa-router objects as parameter and prefixing the directory path to the router. Also we can place in these directories any of the utility function files, modules or helper function modules that we need for the routers.

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago