GETTING STARTED

MAIN CONCEPTS

API

Response handling

You can tell Tuft how to respond to a request by defining a response object as the second argument of .set(). In Tuft, a response object is just a plain JavaScript object with no required properties, so the below example is a perfectly valid route.

app.set('GET /', {})

A response object with no properties will respond to client requests with the default HTTP status code of 200 OK and no response body.

You can send a response body by defining one of the accepted body properties of the response object, such as text.

app.set('GET /', {
  text: 'Hello, world!'
})

Other accepted body properties include raw, html, json, and file. For documentation on how each of these properties works, see the response object section.

Instead of passing a response object, you can also pass a function that returns a response object. The above example could then be written as:

app.set('GET /', () => {
  const text = 'Hello, world!'
  return { text }
})

This is likely how most of your routes will be implemented. However, for any route where the response is known ahead of time, the first method is recommended as it may provide a significant performance improvement, especially for JSON bodies.