ServerResponse

ServerResponse

Creates the response object used to define server status, headers and data.

Constructor

new ServerResponse() → {ServerResponse}

Source:
Example

Using `ServerResponse` within a request event listener

function onRequest (response, request) {
  response.send('<p>some html</p>');
}
Returns:
Type
ServerResponse

Extends

  • http.ServerResponse

Methods

end(dataopt, encodingopt, callbackopt)

Source:

Send the HTTP response status, headers, and body, and end the response. This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

Parameters:
Name Type Attributes Description
data Buffer | Promise | * <optional>

The HTTP response body.

encoding String <optional>

The character encoding of the data.

callback function <optional>

The function called when the stream is finished.

redirect(statusopt, pathopt) → {Server}

Source:

Redirects to a URL path, with a customizable status code. If not specified, status defaults to “302 “Found”.

Parameters:
Name Type Attributes Default Description
status Number <optional>
302

The HTTP response status code.

path String <optional>

The URL path to redirect to.

Returns:
Type
Server

send(statusopt, headersopt, bodyopt) → {Server}

Source:

Send the HTTP response status, headers, and body, and end the response.

Parameters:
Name Type Attributes Default Description
status Number <optional>
200

The HTTP response status code.

headers Number <optional>

The HTTP response headers.

body Buffer | Promise | * <optional>

The HTTP response body.

Returns:
Type
Server

sendCSS(cssopt, optsopt) → {Server}

Source:

Send the HTTP response body as CSS and end the response.

Parameters:
Name Type Attributes Description
css Buffer | Promise | * <optional>

The HTTP response body sent as CSS.

opts Object <optional>

Additional configuration.

Properties
Name Type Attributes Default Description
headers Object <optional>

Additional configuration.

status Number <optional>
200

The HTTP response status code.

Returns:
Type
Server

(async) sendFile(path, optsopt) → {ServerResponse}

Source:

Streams a local file from a given path. Sets Content-Length, Content-Type, Date, and Last-Modified headers.

Parameters:
Name Type Attributes Description
path String

The path used to resolve the local file.

opts Object <optional>

Additional configuration resolving the local file.

Properties
Name Type Attributes Default Description
from Object <optional>
'.'

The directory used to resolve the local file.

index Object <optional>
'index.html'

The index basename used to resolve directories.

headers Object <optional>

Additional HTTP response headers.

use function <optional>

The function used to process the local file.

Returns:

A promise resolving with the current ServerResponse once the file has been streamed.

Type
ServerResponse

sendHTML(htmlopt, optsopt) → {Server}

Source:

Send the HTTP response body as HTML and end the response.

Parameters:
Name Type Attributes Description
html String <optional>

The HTTP response body as HTML.

opts Object <optional>

Additional configuration.

Properties
Name Type Attributes Default Description
headers Object <optional>

Additional configuration.

status Number <optional>
200

The HTTP response status code.

Returns:
Type
Server

sendJS(jsopt, optsopt) → {Server}

Source:

Send the HTTP response body as JavaScript and end the response.

Parameters:
Name Type Attributes Description
js String <optional>

The HTTP response body as JavaScript.

opts Object <optional>

Additional configuration.

Properties
Name Type Attributes Default Description
headers Object <optional>

Additional configuration.

status Number <optional>
200

The HTTP response status code.

Returns:
Type
Server

sendJSON(jsonopt, optsopt) → {Server}

Source:

Send the HTTP response body as JSON and end the response.

Parameters:
Name Type Attributes Description
json * <optional>

The HTTP response to be stringified as JSON.

opts Object <optional>

Additional configuration for stringification.

Properties
Name Type Attributes Default Description
headers Object <optional>

...

replacer function <optional>

The function to customize the JSON stringification process.

space Number | String <optional>
2

The white space used by the JSON stringification process.

status Number <optional>
200

The HTTP response status code.

Returns:
Type
Server

sendType(extension, body, optsopt) → {Server}

Source:

Send the HTTP response status, headers, and body as a specific content type, and end the response.

Parameters:
Name Type Attributes Description
extension Number

The extension used to determine the content-type.

body Buffer | Promise | *

The HTTP response body.

opts Object <optional>

Additional configuration.

Properties
Name Type Attributes Default Description
headers Object <optional>

Additional HTTP response headers.

status Number <optional>
200

The HTTP response status code.

Returns:
Type
Server

setHeader() → {Server}

Source:

Define a to-be-sent header.

Returns:
Type
Server

setHeaders() → {Server}

Source:

Define to-be-sent headers.

Returns:
Type
Server

setStatus(statusopt) → {Server}

Source:

Set the HTTP status for the response.

Parameters:
Name Type Attributes Default Description
status Number <optional>
200

The HTTP response status code.

Returns:
Type
Server

setStatusMessage(messageopt) → {Server}

Source:

Set the status message for the response.

Parameters:
Name Type Attributes Description
message String <optional>

The status message.

Returns:
Type
Server

write(dataopt, encodingopt, callbackopt)

Source:

Send the HTTP response status, headers, and body.

Parameters:
Name Type Attributes Description
data Buffer | Promise | * <optional>

The HTTP response body.

encoding String <optional>

The character encoding of the data.

callback function <optional>

The function called when the data is flushed.

writeHead(status, statusMessageopt, headersopt) → {Server}

Source:

Sends a response header to the request.

Example
response.writeHead(200, {
 'Content-Length': 0,
 contentType: 'text/plain'
})
Parameters:
Name Type Attributes Description
status Number

The HTTP response status code.

statusMessage String <optional>

The status message sent to the client.

headers Object <optional>

Additional HTTP response headers.

Returns:
Type
Server