Middleware and Helpers
Gatsby Functions provides anExpress-like architecture that simplifies buildingNode.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 byadding custom middleware. Data available by default on thereq 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. Thebodycan be astring,object, orbufferres.json(body)— returns a JSON response. Thebodycan be any value that can be serialized withJSON.stringify()res.status(statusCode)— set theHTTP 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
Support for overriding default config added in
gatsby@4.14.0
By default, Gatsby is using following configuration defaults to parse request body and make it available asreq.body field in appropriate format:
Those settings work in most cases, but sometimes you might need to adjust them to cover your use case. Gatsby allows exporting an object namedconfig from your function handler module. This object allows you to control thebody-parser middleware used by Gatsby Functions. Gatsby currently supports thelimit,type, andextended options for thebodyParser configuration, which are documented bybody-parser. Thelimit property will allow configuration of payload size up to32mb.
Examples
Accessing body as aBuffer
You can modify whatContent-type particularbody-parser middleware can act on. Following configuration will force every request to useraw parser and result in function handler receivingreq.body as aBuffer. A setup like this is useful if you are looking to verify signature of webhooks (e.g.https://stripe.com/docs/webhooks/signatures).
Increasing or decreasing the payload limit
By default, the limit is100kb. If the request body is larger than that it will result in automatic413 Request Entity Too Large response without executing function handler at all.
If your use case require a higher limit, you can bump it up inconfig.
TypeScript (config object type)
You can importGatsbyFunctionConfig fromgatsby to type yourconfig export:
Howconfig is applied
When using theconfig and changing thetype on one of the body parser middlewares, it’s important to realize that all body parser middlewares are still being applied with this specific order:
rawtexturlencodedjson
Here’s a concrete example:
If you wantjson to be used for all possible requests for a given function, it won’t be enough to just settype: "*/*" for thejson middleware. You also need to change thetype for middlewares that are higher in priority, so they don’t accidentally match and handle request before thejson middleware can process it: