Skip to main content

API Handler

An API handler function has the following structure:

export default async (req, res) => {
// ...
}

The handler receives a request and response objects. See the sections below for information on the API exposed through these objects.

Requestā€‹

TypeScript Support

A request object has an associated TypeScript type:

  • Type Name - ResolveRequest
  • Package - @resolve-js/runtime-base

The request object exposes the following fields:

FieldDescription
resolveThe reSolve context object that contains reSolve API and metadata.
methodThe request's HTTP method.
pathThe request URL path.
bodyThe request body.
cookiesAn object that contains the attached cookies as key-value pairs.
headersAn object that contains the request's HTTP headers as key-value pairs.
queryAn object that contains the request's query string parameters as key-value pairs.

Responseā€‹

TypeScript Support

A response object has an associated TypeScript type:

  • Type Name - ResolveResponse
  • Package - @resolve-js/runtime-base

The request object exposes the following functions:

FunctionDescription
status(code)Specifies the response status code.
getHeader(key)Gets a response header by key.
setHeader(key, value)Sets a response header.
text([content] [, encoding])Specifies content for a text-type response.
json([content])Specifies content for a JSON-type response.
end([content] [, encoding])Ends the response process.
file(content, filename [, encoding])Specifies a file to send as a response.
redirect([status,] path)Specifies the redirect path.
cookie(name, value [, options])Specifies cookies to send to the client.
clearCookie(name [, options])Clears a cookie from the response.