Skip to content

Commit

Permalink
Improving examples and fixing dep error
Browse files Browse the repository at this point in the history
- Adding more examples of usage in README.md
- Fixing possible peer dependency error with glob module adding glob as dependency
  • Loading branch information
gbahamondezc committed Sep 25, 2017
1 parent a67a952 commit 447430e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
67 changes: 52 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,33 @@ $ yarn add koa-load-routes
```js
const Koa = require('koa');
const loader = require('koa-load-routes');
const authMiddleware = require('./auth-middleware');

// Some module containing business logic and db access
const Interactors = require('./interactors');

var app = new Koa();

/* If path is a directory, loader will read files inside
* to load routes in each file.
*/
// Loading public routes
app.use(route({
path: `${path.resolve()}/src/resources/public`,
recursive: true,
// Add /api/ at start of each loaded endpoint
base: 'api',
// Load files only ending with route.js
suffix: 'route',
// Injecting your business logic module
args: [Interactors]
}));

app.use(loader({
path : __dirname + '/routes.js',
args : [200, {name : 'somename'}],
// base : 'api'
// Loading private routes with auth middleware
app.use(route({
path: `${path.resolve()}/src/resources/account`,
recursive: true,
base: 'api',
suffix: 'route',
middlewares : [authMiddleware()],
args: [Interactors]
}));

app.listen(3000, () => {
Expand All @@ -67,15 +83,36 @@ app.listen(3000, () => {

```

###### routes.js
###### src/resources/public/login.route.js

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

this.get('/', async (ctx, next) => {
await next();
ctx.status = $status;
ctx.body = $body;
})
// Authentication endpoint
this.post('/login', async ctx => {

let userAccount = await Interactors.userAccount
.getByEmail(ctx.request.body.email);

if (!userAccount) {
ctx.throw(404, 'Account not found');
}

if (ctx.body.password !== userAccount.password) {
ctx.throw(403, 'Wrong Email/Password combination');
}

/*
* You can create some kind of auth token here to
* send it in body with user account info.
* This is just an example
*/
ctx.body = userAccount;
});



// Other possible routes examples

this.get('/hello', (ctx, next) => {
ctx.body = 'Hello world';
Expand All @@ -91,7 +128,7 @@ module.exports = function ($status, $body) {
});

// Multiple middlewares
this.get('/multiple', (ctx, next) => {
this.post('/hello4', (ctx, next) => {
console.log('im the first one');
return next();
},
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-load-routes",
"version": "3.0.0",
"version": "3.0.1",
"description": "koa@latest middleware to load routes from files and dirs using koa-router module",
"homepage": "https://github.com/gbahamondez/koa-load-routes",
"author": {
Expand All @@ -13,7 +13,7 @@
],
"main": "lib/loader.js",
"engines": {
"node": ">= 8.2.1",
"node": ">=8.2.1",
"npm": ">=5.3.0"
},
"keywords": [
Expand Down Expand Up @@ -47,6 +47,7 @@
},
"license": "MIT",
"dependencies": {
"glob": "^7.1.2",
"glob-promise": "^3.1.0",
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
Expand Down

0 comments on commit 447430e

Please sign in to comment.