Controllers define the different actions that your users can interact with.
this.request
The raw http.ServerRequest
object for this request/response cycle.
this.response
The raw http.ServerResponse
object for this request/response cycle.
this.params
The parsed params for the request. params
is also passed as an argument to the action, it was added as an instance field for convenience.
this.cookies
Cookies collection from the request
this.name
The name of the controller constructor function, in CamelCase with uppercase initial letter.
this.respondsWith
Content-type the controller can respond with.
this.respondsWith = ['txt','json','html'];
before(filter, [options])
Adds an action to be performed before a response is rendered.
filter [function]
Action to add to the beforeFilter list. If the action is asynchronous, takes a single callback parameter to call when the action is finished.
except [array]
List of actions where the before-filter should not be performed.only [array]
List of actions where the before-filter should only be performed.async
[boolean] When set to true, the before-filter is asynchronous, and requires a callback
this.before(someFunction);
// runs someFunction before the response is rendered
this.before(someFunction, {except: ['index', 'home']});
// won't run someFunction if this is the index or home action
this.before(someFunction, {only: ['add', 'update', 'remove']}
// will only run someFunction if this is the add, update, or remove action
after(filter, [options])
Adds an action to be performed after a response is rendered.
filter [function]
Action to add to the afterFilter list. If the action is asynchronous, takes a single callback parameter to call when the action is finished.
except [array]
List of actions where the after-filter should not be performed.only [array]
List of actions where the after-filter should only be performed.async
[boolean] When set to true, the after-filter is asynchronous, and requires a callback
this.after(someFunction);
// runs someFunction after the response is rendered
this.after(someFunction, {except: ['index', 'home']});
// won't run someFunction if this is the index or home action
this.after(someFunction, {only: ['add', 'update', 'remove']}
// will only run someFunction if this is the add, update, or remove action
protectFromForgery()
Prevents cross-site requests by requiring a same-origin token for destructive HTTP methods (PUT, POST, DELETE)
redirect(to)
- if
to
is a string, it will redirect to the url in that string
controller [string]
: a controller nameaction [string]
: an action nameformat [string]
: the file extension
Sends a 302 redirect to the client, based on either a simple string-URL, or a controller/action/format combination.
this.redirect('/users/1');
// will redirect the browser to /users/1
this.redirect({controller: 'users', action: 'show', id: 1});
// will redirect the browser to /users/1
error(err)
Respond to a request with an appropriate HTTP error-code. If a status-code is set on the error object, uses that as the error's status-code. Otherwise, responds with a 500 for the status-code.
statusCode [number]
optional HTTP status code to send to the client, defaults to 500message [string]
the error message text to send to the client
this.error();
// sends a 500
var err = new Error('Whoopsy daisy');
this.error(err);
// sends a 500 with a specific message
var err = new Error();
err.statusCode = 420;
this.error(err);
// sends a 420
transfer(action)
Transfer a request from its original action to a new one. The entire request cycle is repeated, including before-filters.
action [string]
: name of the new action designated to handle the request.action [object]
: The new action designated to handle the request.
respond(data, options)
Performs content-negotiation, and renders a response.
data [object]
: an object with properties to send to the view
layout [string]
: the path to the layout file to uselayout [false]
: a flag to not use a layout fileformat [string]
: the format to rendertemplate [string]
: The path (without file extensions) to the template to use to render this response
this.respond(params);
// send the params object to the view, then send the response
this.respond({posts: posts});
// send the passed in object to the view, then send the response
this.respond(params, {template: 'path/to/template'});
// send params to path/to/template, render it, then send the response
this.respond(params, {format: 'json'});
// send the params object as the response in json format