RingoJS

Module ringo/jsgi/response

This module provides response helper functions for composing JSGI response objects. For more flexibility the JsgiResponse is chainable.

Functions

Class JsgiResponse

Instance Methods

Instance Properties


JsgiResponse (base)

A wrapper around a JSGI response object. JsgiResponse is chainable.

Example

// Using the constructor
var {JsgiResponse} = require('ringo/jsgi/response');
return (new JsgiResponse()).text('Hello World!').setCharset('ISO-8859-1');

// Using a static helper
var response = require('ringo/jsgi/response');
return response.json({'foo': 'bar'}).error();

Parameters

Object base

a base object for the new JSGI response with the initial status, headers and body properties.


JsgiResponse.prototype. addHeaders (headers)

Merge the given object into the headers of the JSGI response.

Parameters

Object headers

new header fields to merge with the current ones.

Returns

JsgiResponse

JSGI response with the new headers


JsgiResponse.prototype. bad ()

Sets the HTTP status to 400.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. binary (binary, contentType)

Create a JSGI response with a Binary object as response body.

Parameters

ByteString|ByteArray binary

the binary object to write

String contentType

optional MIME type. If not defined, the MIME type is application/octet-stream.

Returns

JsgiResponse

JSGI response


JsgiResponse.prototype. body


JsgiResponse.prototype. created ()

Sets the HTTP status to 201.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. error ()

Sets the HTTP status to 500.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. forbidden ()

Sets the HTTP status to 403.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. gone ()

Sets the HTTP status to 410.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. headers


JsgiResponse.prototype. html (html...)

Set the JSGI response content-type to 'text/html' with the string as response body.

Parameters

String... html...

a variable number of strings to send as response body

Returns

JsgiResponse

JSGI response with content-type 'text/html'


JsgiResponse.prototype. json (object)

Create a JSGI response with content-type 'application/json' with the JSON representation of the given object as response body.

Parameters

Object object

the object whose JSON representation to return

Returns

JsgiResponse

JSGI response with content-type 'application/json'


JsgiResponse.prototype. jsonp (callback, object)

Create a JSGI response with content-type 'application/javascript' with the JSONP representation of the given object as response body wrapped by the callback name.

Parameters

String callback

the callback function name for a JSONP request

Object object

the object whose JSON representation to return

Returns

JsgiResponse

JSGI response with content-type 'application/javascript'


JsgiResponse.prototype. notFound ()

Sets the HTTP status to 404.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. notModified ()

Create a response with HTTP status code 304 that indicates the document has not been modified

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. ok ()

Sets the HTTP status to 200.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. redirect (location)

Create a response with HTTP status code 303 that redirects the client to a new location.

Parameters

String location

the new location

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. setCharset (charsetName)

Set the character encoding used for text responses.

Parameters

String charsetName

the encoding to use.

Returns

JsgiResponse

JSGI response with the given charset


JsgiResponse.prototype. setStatus (code)

Set the JSGI response status. This does not commit the request and continues the JsgiReponse chain.

Parameters

Number code

the status code to use

Returns

JsgiResponse

JSGI response with the new status code


JsgiResponse.prototype. status


JsgiResponse.prototype. stream (stream, contentType)

Create a JSGI response with a stream as response body.

Parameters

Stream stream

the stream to write

String contentType

optional MIME type. If not defined, the MIME type is application/octet-stream.

Returns

JsgiResponse

JSGI response


JsgiResponse.prototype. text (text...)

Set the JSGI response content-type to 'text/plain' with the string as response body.

Parameters

String... text...

a variable number of strings to send as response body

Returns

JsgiResponse

JSGI response with content-type 'text/plain'


JsgiResponse.prototype. unauthorized ()

Sets the HTTP status to 401.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. unavailable ()

Sets the HTTP status to 503.

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. xml (xml)

Create a JSGI response with content-type 'application/xml' with the given XML as response body.

Parameters

XML|String xml

an XML document

Returns

JsgiResponse

JSGI response with content-type 'application/xml'


addHeaders (headers)

Merge the given object into the headers of the JSGI response.

Parameters

Object headers

new header fields to merge with the current ones.

Returns

JsgiResponse

JSGI response with the new headers


bad ()

Sets the HTTP status to 400.

Returns

JsgiResponse

a JSGI response object to send back


created ()

Sets the HTTP status to 201.

Returns

JsgiResponse

a JSGI response object to send back


error ()

Sets the HTTP status to 500.

Returns

JsgiResponse

a JSGI response object to send back


forbidden ()

Sets the HTTP status to 403.

Returns

JsgiResponse

a JSGI response object to send back


gone ()

Sets the HTTP status to 410.

Returns

JsgiResponse

a JSGI response object to send back


html (html...)

Set the JSGI response content-type to 'text/html' with the string as response body.

Parameters

String... html...

a variable number of strings to send as response body

Returns

JsgiResponse

JSGI response with content-type 'text/html'


json (object)

Create a JSGI response with content-type 'application/json' with the JSON representation of the given object as response body.

Parameters

Object object

the object whose JSON representation to return

Returns

JsgiResponse

JSGI response with content-type 'application/json'


jsonp (callback, object)

Create a JSGI response with content-type 'application/javascript' with the JSONP representation of the given object as response body wrapped by the callback name.

Parameters

String callback

the callback function name for a JSONP request

Object object

the object whose JSON representation to return

Returns

JsgiResponse

JSGI response with content-type 'application/javascript'


notFound ()

Sets the HTTP status to 404.

Returns

JsgiResponse

a JSGI response object to send back


notModified ()

Create a response with HTTP status code 304 that indicates the document has not been modified

Returns

JsgiResponse

a JSGI response object to send back


ok ()

Sets the HTTP status to 200.

Returns

JsgiResponse

a JSGI response object to send back


redirect (location)

Create a response with HTTP status code 303 that redirects the client to a new location.

Parameters

String location

the new location

Returns

JsgiResponse

a JSGI response object to send back


setCharset (charsetName)

Set the character encoding used for text responses.

Parameters

String charsetName

the encoding to use.

Returns

JsgiResponse

JSGI response with the given charset


setStatus (code)

Static helper to create a JsgiResponse with the given status code.

Parameters

Number code

the status code to use

Returns

JsgiResponse

JSGI response with the new status code


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.


text (text...)

Set the JSGI response content-type to 'text/plain' with the string as response body.

Parameters

String... text...

a variable number of strings to send as response body

Returns

JsgiResponse

JSGI response with content-type 'text/plain'


unauthorized ()

Sets the HTTP status to 401.

Returns

JsgiResponse

a JSGI response object to send back


unavailable ()

Sets the HTTP status to 503.

Returns

JsgiResponse

a JSGI response object to send back


xml (xml)

Create a JSGI response with content-type 'application/xml' with the given XML as response body.

Parameters

XML|String xml

an XML document

Returns

JsgiResponse

JSGI response with content-type 'application/xml'