Class: ServiceResponse

x2node-ws~ ServiceResponse

Web-service response. An instance is usually returned by an enpoint handler and is used by the framework to construct the HTTP response and send it back to the client.

new ServiceResponse(statusCode)

Note: The constructor is not accessible from the client code. Instances are created using module's createResponse() function.
Parameters:
Name Type Description
statusCode number HTTP response status code.

Members


<readonly> entities :Array.<module:x2node-ws~ServiceResponse~Entity>

All response entities and attachments in the correct order, or empty array if none.
Type:

<readonly> headers :Object.<string, string>

HTTP response headers. All header names are lowercase.
Type:
  • Object.<string, string>

<readonly> statusCode :number

HTTP response status code.
Type:
  • number

Methods


addAttachment(data [, contentType] [, filename])

Add attachment to the response. Multiple attachments can be added to a response with or without the main entity.

If a response has entity and attachments or just more than one attachment, the HTTP response is sent with content type "multipart/mixed". The parts are included in the response payload in the order they were added with the main entity, if any, always first.

As with the main entity, the attachment data can be provided as an object, as a buffer or as a readable stream (which will trigger "chunked" HTTP response encoding).

Parameters:
Name Type Argument Default Description
data Object | external:Buffer | stream.external:Readable The attachment data.
contentType string <optional>
application/json Content type. If omitted, "application/json" is assumed.
filename string <optional>
Optional filename associated with the attachment.
Returns:
This response object.
Type
module:x2node-ws~ServiceResponse

addToHeadersListHeader(name, value)

Add value(s) to an HTTP response header that is a list of other header names. Examples of such headers are "Vary", "Access-Control-Allow-Headers" and "Access-Control-Expose-Headers". The method checks if the headers are already present in the current value and does not add them twice.
Parameters:
Name Type Description
name string HTTP response header name, case-insensitive.
value string | Array.<string> Header(s) to add. Multiple headers can be specified as an array of in a comma-separated string. The case of the headers is not important as all the values are automatically normalized.
Returns:
This response object.
Type
module:x2node-ws~ServiceResponse

addToMethodsListHeader(name, value)

Add value(s) to an HTTP response header that is a list of HTTP methods. Examples of such headers are "Allow" and "Access-Control-Allow-Methods". The method checks if the methods are already present in the current value and does not add them twice.
Parameters:
Name Type Description
name string HTTP response header name, case-insensitive.
value string | Array.<string> Method(s) to add. Multiple methods can be specified as an array or in a comma-separated string. The case of the methods is not important as all the values are automatically upper-cased.
Returns:
This response object.
Type
module:x2node-ws~ServiceResponse

hasHeader(name)

Tell if the response contains the specified HTTP response header.
Parameters:
Name Type Description
name string HTTP response header name, case-insensitive.
Returns:
true if contains the header.
Type
boolean

setEntity(data [, contentType])

Add main entity to the response. Only one entity can be added to a response.

The entity data can be provided in one of the three forms an object, a buffer or a readable stream. If an object is provided, it is serialized according to the contentType argument. If buffer is provided, its binary data is sent as-is. In either of these cases the response is sent synchronously all at once. However, if data is provided as a stream, the response is sent asynchronously using HTTP "chunked" transfer encoding.

Parameters:
Name Type Argument Default Description
data Object | external:Buffer | stream.external:Readable The entity data.
contentType string <optional>
application/json Content type. If omitted, "application/json" is assumed.
Returns:
This response object.
Type
module:x2node-ws~ServiceResponse

setHeader(name, value)

Add header to the HTTP response. Any existing value is replaced.
Parameters:
Name Type Description
name string HTTP response header name, case-insensitive.
value * The value. If the value is a Date, it is converted to string using Date.toUTCString() method. Otherwise, String() is used to convert it to string. If value is null, any existing header is removed instead.
Returns:
This response object.
Type
module:x2node-ws~ServiceResponse

Type Definitions


Entity

HTTP entity descriptor.
Type:
  • Object
Properties:
Name Type Description
headers Object.<string, string> HTTP response headers associated with the entity (such as "content-type" and "content-disposition"). All header names are lowercase.
data Object | external:Buffer | stream.external:Readable The entity data.