This document is now deprecated. Please instead head here.
Text Routes
Overview
Begin text routes respond with HTTP Content-Type: text/plain
, and support routes with GET
.
Each text route (example: GET /robots.txt
) in your app is assigned a folder in your project under src/text/
(i.e. src/text/get-000param/
).
Within your project, each route can contain and utilize an arbitrary quantity of modules, packages, and other files (so long as the total uncompressed size of that route's folder is ≤5MB).
Note: Begin routes are plain AWS Lambda functions, and can function without requiring
@architect/functions
. However, we do not suggest removing that require, as you will lose middleware and session support.
GET
routes
Let's look at the default code for new text GET
routes:
// src/text/get-*/index.js
let begin = require('@architect/functions')
function route(req, res) {
console.log(JSON.stringify(req, null, 2))
res({
text: 'hello world'
})
}
exports.handler = begin.text.get(route)
begin.text.get()
Invoked by the route's handler
, begin.text.get()
accepts one or more functions that follow an Express-style middleware signature: (req, res, next)
Parameters
req
req
returns a JavaScript object with the following keys:
method
- HTTP method (always returnsget
)path
- path requested (i.e./robots.txt
)headers
- object containing HTTP request headersquery
- object containing query string fields & valuesbody
- always returns empty objectparams
- object containing path params (returned empty unless your route contains params)session
- object containing session datacsrf
- signed cross-site request forgery token (generated with all requests, but primarily intended to be used with HTMLPOST
routes)
res()
res()
is a function that must be invoked; it accepts a JavaScript object with the following keys:
- Either
text
orlocation
(required)text
- a string containing text contentlocation
- a URL, either absolute or relative; sets HTTP status to302
without using thestatus
key
session
(optional) - object containing session datastatus
(optional) - alternatelycode
orstatusCode
, sets HTTP error status code, supports the following values:400
- Bad Request403
- Forbidden404
- Not Found406
- Not Acceptable409
- Conflict415
- Unsupported Media Type500
- Internal Server Error
res()
can also be invoked with an instance of Error
. You can also optionally define the Error
object's HTTP status code by adding to it a status
, code
, or statusCode
property (with one of the seven status codes above).
next
(optional)
Callback argument to continue execution.
GET
examples
Examples coming shortly, please stand by!