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
const {JsgiResponse} = require('ringo/jsgi/response');
return (new JsgiResponse()).text('Hello World!').setCharset('ISO-8859-1');

// Using a static helper
const 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. Note that header field names are case-sensitive in the JsgiResponse object, whereas they are not in the final HTTP response message sent out by Jetty.

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 (data, contentType)

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

Parameters

ByteString|ByteArray data

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. setContentType (contentType)

Set the content type for the current response.

Parameters

String contentType

the content type header value.

Returns

JsgiResponse

JSGI response with the given charset


JsgiResponse.prototype. setHeaders (headers)

Merge the given object into the headers of the JSGI response. Existing headers in the to-be-sent headers will be replaced with the new ones. Note that header field names are case-sensitive in the JsgiResponse object, whereas they are not in the final HTTP response message sent out by Jetty.

Parameters

Object headers

new headers to be set

Returns

JsgiResponse

JSGI response with the new headers


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


range (request, representation, size, contentType, timeout, maxRanges)

An async response representing a resource as a single or multiple part response. Multiple or overlapping byte ranges are coalesced into a canonical response range.

Parameters

Object request

a JSGI request object

String|Resource|Stream representation

path of a file as string, a resource, or a readable io.Stream

Number size

optional size of the resource in bytes, -1 indicates an unknown size.

String contentType

optional content type to send for single range responses

Number timeout

optional timeout to send back the ranges, defaults to 30 seconds, -1 indicates an infinite timeout.

Number maxRanges

optional maximum number of ranges in a request, defaults to 20. Similar to Apache's MaxRanges directive.

Returns

AsyncResponse

async response filled with the give ranges


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


setContentType (contentType)

Set the content type for the current response.

Parameters

String contentType

the content type header value.

Returns

JsgiResponse

JSGI response with the given charset


setHeaders (headers)

Merge the given object into the headers of the JSGI response. Existing headers in the to-be-sent headers will be replaced with the new ones.

Parameters

Object headers

new headers to be set

Returns

JsgiResponse

JSGI response with the new headers


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'