Skip to content

Instantly share code, notes, and snippets.

@brenolf
Last active December 16, 2015 18:37
Show Gist options
  • Save brenolf/c5d7cd162d8b81d96f0d to your computer and use it in GitHub Desktop.
Save brenolf/c5d7cd162d8b81d96f0d to your computer and use it in GitHub Desktop.
ES6 + co express example
'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))
}
}
'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]])
}
'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