Controllers for Node.js (originally built to be used with Connect / Express.js)
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Controller for Connect and Express (Node.js / Express.js / Connect.js). Name comes from Chungking Express.


npm install chungking


Set up a Users controller

// users.js
var BaseController = require('chungking').BaseController;

var Users = BaseController.extend(function() {
  // this === Users.prototype

  // Before filters are called whenever you call an action.
  // They are asynchronous so you need to call
  // when you are done.
  this.beforeFilter(function() {
    console.log('This is called before any action is called.');
      this.res.send('Not authorized');

  // You can also add methods that are available to all your
  // actions.
  this.authenticate = function() {
    console.log('You can put some authentication code here.');
    return true;

  // Add actions below
  this.action('index', function() {
    // this.req, this.res, are available anywhere in
    // your controller.
    this.res.send('Users index.');


module.exports = Users;

Call the controller from your request handler:

// app.js
var Users = require('./users');

// ...

app.get('/users', function(req, res, next) {
  var ctl = new Users(req, res, next);

That's it!

Restricting beforeFilters to only specific actions

this.authorize = function() {
  console.log('This is called before any action is called.');
  //authentication code here...

// restrict to only some actions
this.beforeFilter({only: ['edit', 'update', 'create']}, function() {

// execute for all actions except listed actions
this.beforeFilter({except: ['show']}, function() {

Tired of writing this.req, this.res and

You can optionally get them as params to your beforeFilters and actions.

this.beforeFilter(function(req, res, next) {
  res.send('aaahhh much better :)');

Extending controllers

You can also extend controllers.

Application controller:

// application.js
var BaseController = require('chungking').BaseController;

var Application = BaseController.extend(function() {
  // This before filter will be called whenever a controller that
  // extends Application gets an action called.
  this.beforeFilter(function() {
    if(!this.req.user) this.res.send('Unauthorized!');

  // Set up some useful aliases
  this.beforeFilter(function() {
    this.currentSection = 'Application';

  // This action will be available to any controller that extends
  // the Application controller.
  this.action('whoami', function() {

module.exports = Application;

Users controller which extends the Application controller:

// users.js
var Application = require('./application');

var Users = Application.extend(function() {
  this.beforeFilter(function() {
    console.log('This will be called after the Application'
      + 'controller\'s before filters.');;

  this.action('index', function() {
    this.res.send('We are in the section ' + this.currentSection);


Rethink how inheritance works. Maybe child controllers should not inherit before filters and actions.

Refactor the way inheritance is done. Maybe use utils.inherit from node.js