Create lambda apis easily
- inspired by Fastify and lambda-api
// Initialize app
const lambdaify = require('lambdaify')()
// Register route
lambdaify.get('/get/:id', (req, res) => {
return res
.code(200)
.header("key", "value")
.send('hello from lambdaify!')
})
// Execute event
exports.handler = (event, context) => {
return lambdaify.run(event, context);
};
GET, PUT, POST, and DELETE methods can be registered by invoking the convenience methods on the app.
To add a route, simply provide a path and callback. The path must be a string. To indicate a path parameter, prefix the ontology with a :
. The callback must be a function that accepts two params, req and res.
Example usage is below
const lambdaify = require('lambdaify')()
lambdaify.get('/get', (req, res) => {
return res.send('get example')
})
lambdaify.post('/post', (req, res) => {
return res.send('post example')
})
// 'id' is a path param whose value can be accessed using req.id
lambdaify.put('/put/:id', (req, res) => {
return res.send('put example')
})
// 'id' and 'index' are path params whose values can be accessed using req.params.id and req.params.index
lambdaify.put('/delete/:id/:index', (req, res) => {
return res.send('put example')
})
req.raw
: (object) Returns the raw alb eventreq.method
: (string) Returns the http verb of the eventreq.path
: (string) Returns the raw path of the eventreq.queryStringParameters
: (object) Returns the query path params of the eventreq.headers
: (object) Returns the headers of the eventreq.body
: (string) Returns the body of the eventreq.isBase64Encoded
: (boolean) Returns a boolean detailing the base64Encoded status of the eventreq.params
: (object) Returns the path params of the event
-
res.send()
: Sets the response body. This should only be called once. Ifsend
is called in the context of middleware, the execution stack will break and the response will be returned immediately.res.send({ test: 'response' })
-
res.header() | res.setHeader()
: Sets a header key value pair. NOTE: Header keys will be converted to lowercase.res.header('key', 'value')
-
res.setSerializer()
: Sets a serializer function that will be used to parse the response before it is sent. If no serializer is defined, body will be stringified. NOTE: If base64Encoding is defined, encoding will happen after body has been serializedres.setSerializer(() => 'serializer')
-
res.code() | res.status()
: Sets status code for the response. Status codes must be an integer between 100-600res.code(200) | res.status(200)
-
res.encodeBase64
: base64 encodes response bodyres.encodeBase64()
To add global middleware see the example usage below. Global middleware will be executed before route handlers and will be executed in the order they are added.
const lambdaify = require('lambdaify')()
const middleware = (req, res, next) => {
res.send('Middleware')
}
lambdaify.use(middleware)
To add path specific middleware, see the example usage below.
const lambdaify = require('lambdaify')()
const middleware = (req, res, next) => {
res.send('Middleware')
}
lambdaify.use('/test', middleware)
EX: This will match all routes that start with test
as the first path parameter like /test/example
and /test/example/example2
Wildcard middleware paths are also supported using the *
symbol. Example usage is shown below
const lambdaify = require('lambdaify')()
const middleware = (req, res, next) => {
res.send('Middleware')
}
lambdaify.use('/*/example', middleware)
EX: This will match any routes with example
as the second path parameter like /test123/example
and /anything/example/example2