Last active
December 16, 2015 18:37
-
-
Save brenolf/c5d7cd162d8b81d96f0d to your computer and use it in GitHub Desktop.
ES6 + co express example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// using bookshelf.js | |
const Model = require('../models/Model') | |
module.exports = class Controller { | |
static *read (request, response) { | |
const count = yield Model.org(request.organization).count() | |
response.ok() | |
response.json(parseInt(count)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const wrench = require('wrench') | |
const co = require('co-express') | |
const path = require('path') | |
let controllers = {} | |
const PATH = path.resolve(__dirname, '../controllers') | |
wrench.readdirSyncRecursive(PATH) | |
.forEach(file => { | |
let name = file.split('.')[0] | |
controllers[name] = require(`${PATH}/${file}`) | |
}) | |
module.exports = (controller) => { | |
controller = controller.split('.') | |
return co(controllers[controller[0]][controller[1]]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// a tweak to import all controllers in an easy fashion | |
const co = require('./utils/router_handler') | |
// imported after express is defined | |
const app = module.parent.exports | |
// Controller | |
app | |
.get('/myroute', co('Controller.read')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment