Middleware and Helpers
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. You can parse more by adding custom middleware. Data available by default on the req
object:
- Cookies at
req.cookies
- URL Queries (e.g.
api/foo?query=foo
) atreq.query
- Form parameters and data at
req.body
- JSON POST bodies at
req.body
- Files uploaded from forms at
req.files
Response helpers
res.send(body)
— returns the response. Thebody
can be astring
,object
, orbuffer
res.json(body)
— returns a JSON response. Thebody
can be any value that can be serialized withJSON.stringify()
res.status(statusCode)
— set the HTTP status for the response. Defaults to200
.res.redirect([statusCode], url)
— Returns a redirect to a URL. Optionally set the statusCode which defaults to302
.
Custom middleware
Custom Connect/Express middleware are supported.
An example of how to add CORS support to a Function:
Custom body parsing
Generally useful for file upload support.
This is not yet supported. Add a comment in the discussion if this is an important use case for you.