Socket
Book a DemoInstallSign in
Socket

koa-folder-routes

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-folder-routes

Create [koa-router](https://github.com/alexmingoia/koa-router) objects with folder path appended.

1.0.13
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

koa-folder-routes

Create koa-router objects with folder path appended.

Eg:

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.

Keywords

koa

FAQs

Package last updated on 22 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.