GETTING STARTED

MAIN CONCEPTS

API

Response object

The Tuft response object is an ordinary JavaScript object with no required properties. When you add a route, you either define a response object, or you define a function that returns a response object (a response handler). Tuft uses the properties of said object to determine how to respond to a client request.

const response = {
  status: 200,
  text: 'Hello, world!'
}

app.set('GET /', response)

// or

app.set('GET /', () => {
  return response
})

Tuft is able to handle the following response object properties (all optional):

statusnumber

Sets the outgoing HTTP status code to the given value.

Defaults to 200.

errorstring

Responds to the client with an HTTP error status code, the value of which is determined by the provided string. The string should be the name of an HTTP error in all uppercase, with any spaces separated by an underscore.

For example, if error is set to 'NOT_FOUND', the server will respond with a 404 Not Found status code.

All valid 4xx and 5xx codes are supported. If an unsupported string value is provided, then the server will respond with 400 Bad Request.

redirectstring

Responds to the client with a 302 Found status code and a 'location' header set to the provided value, which can either be a relative path or an absolute URI.

rawbuffer

Sets the response body to the provided value and adds an outgoing 'content-type' header of 'application/octet-stream'.

textstring | number | boolean

Sets the response body to the provided value and adds an outgoing 'content-type' header of 'text/plain; charset=UTF-8'.

htmlstring

Sets the response body to the provided value and adds an outgoing 'content-type' header of 'text/html; charset=UTF-8'.

jsonstring | object

Serializes the provided value to a JSON string (if it is already a string, then it is not modified), then sets the response body to that string. Adds an outgoing 'content-type' header of 'application/json; charset=UTF-8'.

filestring

Responds to the client with a file, where file is a pathname. Adds an outgoing 'content-type' header of 'text/plain; charset=UTF-8'.

By default, any properties not listed above will be ignored. The status property is the only one that can be used in conjunction with other properties (with the exception of error and redirect).

If a response object with none of the supported properties is provided, the server will respond to the client with a 200 OK status code and no response body.

If a Tuft responder has been defined on a given route, then it may accept other properties not listed here, or even override them. See the section on responders for more information.