Skip to content

Instantly share code, notes, and snippets.

@moll
Last active January 2, 2016 08:09
Show Gist options
  • Save moll/8274610 to your computer and use it in GitHub Desktop.
Save moll/8274610 to your computer and use it in GitHub Desktop.
World's smallest useful Node.js Express web controller library you can do. Say no to frameworks!
var inherit = require("descend")
var Controller = module.exports = function(req, res, next) {
this.req = req
this.res = res
this.next = next
}
Controller.inherit = function() {
var heir = inherit.apply(this, arguments)
_.extend(heir, this)
return heir
}
Controller.action = function(name) {
var self = this
return function(req, res, next) {
new self(req, res, next)[name](req, res, next)
}
}
@moll
Copy link
Author

moll commented Jan 5, 2014

Use it as such:

var FooController = module.exports = Controller.inherit()

FooController.route = function(app) {
  app.get("/foo", this.action("index")
}

FooController.prototype.index = function(req, res, next) {
  res.send("Hello!")
}

Then pass Express.js's app to FooController.route and you're good to go. Again, steer clear of frameworks for frameworks' sake!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment