rsnext/docs/api-routes/response-helpers.md

1.5 KiB

description
API Routes include a set of Express.js-like methods for the response to help you creating new API endpoints. Learn how it works here.

Response Helpers

Examples

The response (res) includes a set of Express.js-like methods to improve the developer experience and increase the speed of creating new API endpoints, take a look at the following example:

export default function handler(req, res) {
  res.status(200).json({ name: 'Next.js' })
}

The included helpers are:

  • res.status(code) - A function to set the status code. code must be a valid HTTP status code
  • res.json(body) - Sends a JSON response. body must be a serialiazable object
  • res.send(body) - Sends the HTTP response. body can be a string, an object or a Buffer
  • res.redirect([status,] path) - Redirects to a specified path or URL. status must be a valid HTTP status code. If not specified, status defaults to "307" "Temporary redirect".

To view an example using types, check out the TypeScript documentation.