Gatsby Functions provides an Express-like architecture that simplifies building Node.js APIs. We include a number of middlewares to parse common request data as well as response helpers.

Data formats

We parse commonly used data types. Available on the req object:

  • Cookies at req.cookies
  • URL Queries (e.g. api/foo?query=foo) at req.query
  • Form parameters and data at req.body
  • JSON POST bodies at req.body

Response helpers

  • res.send(body) — returns the response. The body can be a string, object, or buffer
  • res.json(body) — returns a JSON response. The body can be any value that can be seralized with JSON.stringify()
  • res.status(statusCode) — set the HTTP status for the response. Defaults to 200.
  • res.redirect([statusCode], url) — Returns a redirect to a URL. Optionally set the statusCode which defaults to 302.

Custom middleware

Custom Connect/Express middleware are supported.

An example of how to add CORS support to route:

Custom body parsing

This is not yet supported. Add a comment in the discussion if this is an important use case for you.