脚本开发

模块 ringo/jsgi/response

该模块提供了用于组成 JSGI 响应对象的响应帮助函数。为了更加灵活,JsgiResponse 是可链接的。

Functions

Class JsgiResponse

Instance Methods

Instance Properties


JsgiResponse (base)

一个 JSGI 响应对象的包装器。 JsgiResponse 是可链接的。

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)

将给定的对象合并到 JSGI 响应的头文件中。请注意,标题字段名称在 JsgiResponse 对象中区分大小写,但它们不在 Jetty 发送的最终 HTTP 响应消息中。

Parameters

Object headers

new header fields to merge with the current ones.

Returns

JsgiResponse

JSGI response with the new headers


JsgiResponse.prototype. bad ()

将 HTTP 状态设置为 400。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. binary (binary, contentType)

用 Binary 对象创建一个 JSGI 响应作为响应主体。

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 ()

将 HTTP 状态设置为 201。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. error ()

将 HTTP 状态设置为 500。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. forbidden ()

将 HTTP 状态设置为 403。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. gone ()

将 HTTP 状态设置为 410。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. headers


JsgiResponse.prototype. html (html...)

将 JSGI 响应内容类型设置为“text / html”,并将字符串作为响应主体。

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)

用给定对象的 JSON 表示作为响应主体,创建一个带有内容类型 'application / json' 的 JSGI 响应。

Parameters

Object object

the object whose JSON representation to return

Returns

JsgiResponse

JSGI response with content-type 'application/json'


JsgiResponse.prototype. jsonp (callback, object)

创建一个 JSGI 响应,其内容类型为 'application / javascript',给定对象的 JSONP 表示作为由回调名称包装的响应主体。

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 ()

将 HTTP 状态设置为 404。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. notModified ()

使用 HTTP 状态代码 304 创建一个响应,指示文档未被修改。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. ok ()

将 HTTP 状态设置为 200。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. redirect (location)

使用 HTTP 状态代码 303 创建一个响应,将客户端重定向到新的位置。

Parameters

String location

the new location

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. setCharset (charsetName)

设置用于文本响应的字符编码。

Parameters

String charsetName

the encoding to use.

Returns

JsgiResponse

JSGI response with the given charset


JsgiResponse.prototype. setContentType (contentType)

设置当前响应的内容类型。

Parameters

String contentType

the content type header value.

Returns

JsgiResponse

JSGI response with the given charset


JsgiResponse.prototype. setHeaders (headers)

将给定的对象合并到JSGI响应的头文件中。待发送头文件中的现有头文件将被替换为新头文件。请注意,标题字段名称在 JsgiResponse 对象中区分大小写,但它们不在 Jetty 发送的最终 HTTP 响应消息中。

Parameters

Object headers

new headers to be set

Returns

JsgiResponse

JSGI response with the new headers


JsgiResponse.prototype. setStatus (code)

设置 JSGI 响应状态。这不会提交请求并继续 JsgiReponse 链。

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)

用流作为响应主体创建一个JSGI响应

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...)

将 JSGI 响应内容类型设置为“text / plain”,并将字符串作为响应主体。

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 ()

将 HTTP 状态设置为 401。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. unavailable ()

将 HTTP 状态设置为 503。

Returns

JsgiResponse

a JSGI response object to send back


JsgiResponse.prototype. xml (xml)

使用给定的 XML 作为响应主体,创建一个带有内容类型“application / xml”的 JSGI 响应。

Parameters

XML|String xml

an XML document

Returns

JsgiResponse

JSGI response with content-type 'application/xml'


addHeaders (headers)

将给定的对象合并到 JSGI 响应的头文件中。

Parameters

Object headers

new header fields to merge with the current ones.

Returns

JsgiResponse

JSGI response with the new headers


bad ()

将 HTTP 状态设置为 400。

Returns

JsgiResponse

a JSGI response object to send back


created ()

将HTTP状态设置为201。

Returns

JsgiResponse

a JSGI response object to send back


error ()

将 HTTP 状态设置为 500。

Returns

JsgiResponse

a JSGI response object to send back


forbidden ()

将 HTTP 状态设置为 403。

Returns

JsgiResponse

a JSGI response object to send back


gone ()

将HTTP状态设置为410。

Returns

JsgiResponse

a JSGI response object to send back


html (html...)

将 JSGI 响应内容类型设置为“text / html”,并将字符串作为响应主体。

Parameters

String... html...

a variable number of strings to send as response body

Returns

JsgiResponse

JSGI response with content-type 'text/html'


json (object)

用给定对象的JSON表示作为响应主体,创建一个带有内容类型'application / json'的JSGI响应。

Parameters

Object object

the object whose JSON representation to return

Returns

JsgiResponse

JSGI response with content-type 'application/json'


jsonp (callback, object)

创建一个 JSGI 响应,其内容类型为'application / javascript',给定对象的 JSONP 表示作为由回调名称包装的响应主体。

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 ()

将 HTTP 状态设置为 404。

Returns

JsgiResponse

a JSGI response object to send back


notModified ()

使用 HTTP 状态代码 304 创建一个响应,指示文档未被修改。

Returns

JsgiResponse

a JSGI response object to send back


ok ()

将 HTTP 状态设置为 200。

Returns

JsgiResponse

a JSGI response object to send back


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

表示资源为单个或多个部分响应的异步响应。多个或重叠的字节范围合并为一个规范的响应范围。

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)

使用 HTTP 状态代码 303 创建一个响应,将客户端重定向到新的位置。

Parameters

String location

the new location

Returns

JsgiResponse

a JSGI response object to send back


setCharset (charsetName)

设置用于文本响应的字符编码。

Parameters

String charsetName

the encoding to use.

Returns

JsgiResponse

JSGI response with the given charset


setContentType (contentType)

设置当前响应的内容类型。

Parameters

String contentType

the content type header value.

Returns

JsgiResponse

JSGI response with the given charset


setHeaders (headers)

将给定的对象合并到 JSGI 响应的头文件中。待发送头文件中的现有头文件将被替换为新头文件。

Parameters

Object headers

new headers to be set

Returns

JsgiResponse

JSGI response with the new headers


setStatus (code)

使用给定的状态码创建 JsgiResponse 的静态帮助器。

Parameters

Number code

the status code to use

Returns

JsgiResponse

JSGI response with the new status code


static (resource, contentType)

表示静态资源的响应。

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...)

将 JSGI 响应内容类型设置为“text / plain”,并将字符串作为响应主体。

Parameters

String... text...

a variable number of strings to send as response body

Returns

JsgiResponse

JSGI response with content-type 'text/plain'


unauthorized ()

将HTTP状态设置为401。

Returns

JsgiResponse

a JSGI response object to send back


unavailable ()

将 HTTP 状态设置为 503。

Returns

JsgiResponse

a JSGI response object to send back


xml (xml)

使用给定的 XML 作为响应主体,创建一个带有内容类型“application / xml”的JSGI响应。

Parameters

XML|String xml

an XML document

Returns

JsgiResponse

JSGI response with content-type 'application/xml'