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. The- bodycan be a- string,- object, or- buffer
- res.json(body)— returns a JSON response. The- bodycan be any value that can be serialized 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 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.