Skip to content

Controllers

jarnoux edited this page Jul 15, 2013 · 7 revisions

Controllers are a special kind of resource. They are functions that are used by the dispatcher to process a request and generate an object to be injected in the template by the template engine. They have the following signature.

function myController(request, response, done)

The done function is to be called when the controller has finished processing and is ready to yield its result. It's a classic nodejs callback function with the following signature.

function done(error, result)

In general it's a bad idea to write on the response in a controller since this will flush the headers and it is likely that some middleware downstream will try to do that too (especially the dispatcher if some controller returns an error), causing an exception.

Example:

/** controllers/myController.js */

module.exports = function (config) {
    return function myController(req, res, done) {
        done(null, {
            message: "Hello, " + req.username
        });
    }
}

Clone this wiki locally