Module ringo/webapp/response
This module provides a Response helper classes for composing JSGI response objects. The Response class also features a number of static methods for composing special-purpose response objects.
Response ()
A Response helper class for composing text-based HTTP responses. This class also provides a number of static helper methods for composing special-purpose response objects.
Response.error (msg)
Create a 500 error response.
Parameters
| String | msg | the message of response body |
Response.prototype.addHeader (key, value)
Add a header to be sent to the client. If a header with this name was previously set it will not be replaced.
Parameters
| String | key | the header name |
| String | value | the header value |
Returns
| Response | this response for chainability |
Response.prototype.body
Property to get or set the response body. While this property can
be set to any valid JSGI response body, the write and writeln
methods of this response will only work if the response has a write
method taking one or more string arguments.
Response.prototype.charset
The character encoding of the response.
Response.prototype.contentType
The Content-Type of the response.
Response.prototype.getHeader (key)
Get the response header value for a given key.
Parameters
| key | {String} the header name |
Returns
| String | the header value |
Response.prototype.headers
Property to get or set the response headers.
Response.prototype.render (skin, context)
Render a skin to the response's buffer
Parameters
| skin | path to skin resource | |
| context | context object |
Response.prototype.setCookie (key, value, days, options)
Sets a cookie to be sent to the client. All arguments except for key and value are optional. The days argument specifies the number of days until the cookie expires. To delete a cookie immediately, set the days argument to 0. If days is undefined or negative, the cookie is set for the current browser session.
Example
res.setCookie("username", "michi");
res.setCookie("password", "strenggeheim", 10,
{path: "/mypath", domain: ".mydomain.org"});
Parameters
| String | key | the cookie name |
| String | value | the cookie value |
| Number | days | optional the number of days to keep the cookie. If this is undefined or -1, the cookie is set for the current session. If this is 0, the cookie will be deleted immediately. |
| Object | options | optional options argument which may contain the following properties:
|
Returns
| Response | this response object for chainability; |
Response.prototype.setHeader (key, value)
Set a header to be sent to the client. If a header with this name was previously set it will be replaced.
Parameters
| String | key | the header name |
| String | value | the header value |
Returns
| Response | this response for chainability |
Response.prototype.status
The HTTP status code of the response.
Response.prototype.write ()
Append one or more arguments to the response body.
Response.prototype.writeln ()
Append one or more arguments to the response body,
followed by a '\r\n' sequence.
Response.json (object)
Create a response object containing the JSON representation of an object.
Parameters
| Object | object | the object whose JSON representation to return |
Response.notFound (location)
Create a 404 not-found response.
Parameters
| String | location | the location that couldn't be found |
Response.redirect (location)
Create a response that redirects the client to a different URL.
Parameters
| String | location | the new location |
Response.skin (skin, context)
A response object rendered from a skin.
Parameters
| Resource|String | skin | the skin resource or path |
| Object | context | the skin context object |
Response.static (resource, contentType)
A response representing a static resource.
Parameters
| String|Resource | resource | the resource to serve |
| String | contentType | optional MIME type. If not defined, the MIME type is detected from the file name extension. |
Response.xml (xml)
Create a response containing the given XML document
Parameters
| XML|String | xml | an XML document |
