lambda-api-router
v1.0.6
Published
Write serverless code like a traditional express application
Downloads
154
Maintainers
Readme
Lamb Chop
Lamb Chop provides an elegant and expressive syntax for using a single Lambda function as a proxy service for API Gateway routes. Write serverless code in a server oriented manner.
Getting Started
Before you can use this service make sure you setup your AWS API Gateway as a proxy service. You can create a single route which accepts any http request
and path using the special /{path+}
. Please see this AWS document for more
information on setting up your lambda function as a proxy service.
Installing
You can install this package through NPM with
npm i --save lambda-api-router
Check out the following example for using the service in a live system:
const Api = require('lambda-api-router');
const app = new Api();
app.get('/foo/bar', (req, res) => {
res.json({ success: true, test: 'Its working!', response: response.substring(0, 21) });
});
app.post('/foo/bar', (req, res) => {
// ...
});
app.put('/users/find', (req, res) => {
// ...
});
app.delete('/user/:id', (req, res) => {
// ...
});
exports.handler = async (event, context) => app.listen(event, context);
Async Actions
You can also use asynchronous actions and promises within your route handlers:
const Api = require('lambda-api-router');
const request = require('request-promise-native');
const app = new Api();
app.get('/foo/bar', async (req, res) => {
const response = await request('http://google.com');
res.json({ success: true, response: response.substring(0, 21) });
});
exports.handler = (event, context) => app.listen(event, context);
Query & Request Params
You can easily access query and request parameters through req.query
and req.params
respectively.
const Api = require('lamb-chop');
const app = new Api();
app.get('/foo/bar', (req, res) => {
res.json({ query: req.query, params: req.params });
});
exports.handler = (event, context) => app.listen(event, context);
Set Response Status & Headers
You can easily access query and request parameters through req.query
and req.params
respectively.
const Api = require('lambda-api-router');
const app = new Api();
app.get('/foo/bar', (req, res) => {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json'
};
res.headers(headers)
.status(200)
.json({ query: req.query, params: req.params });
});
exports.handler = (event, context) => app.listen(event, context);
Set Response Status & Headers
You can also add middleware to your routes. Middleware is simply a function which has access to the request and response objects which execute before the actual route handler is executed. Middleware is useful for things like checking authentication, adding headers, logging and other common web tasks.
You can register middleware using the use
function like so:
const Api = require('lambda-api-router');
const jwt = require('jsonwebtoken');
const app = new Api();
// Logging Middleware
app.use((req, res) => {
console.log(`[${req.httpMethod}] -- ${req.path} --`);
});
// Custom header Middleware
app.use((req, res) => {
res.headers({
'X-Custom-Header': 'my-custom-value',
});
});
// Authentication Middleware
app.use((req, res) => {
const token = req.body.token;
jwt.verify(token, 'shhhhh', function(err, decoded) {
console.log(decoded.foo) // bar
// if decoded.foo => proceed with request
// else return res.error({ ... });
});
});
app.get('/foo/bar', (req, res) => {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json'
};
res.headers(headers)
.status(200)
.json({ query: req.query, params: req.params });
});
exports.handler = (event, context) => app.listen(event, context);
Deployment
This is deployed through the deployment script called: ./scripts/publish.sh
.
Built With
- Node.JS - The web framework used
- NPM - Dependency Management
- Javascript - Programming language used
- Lambda - Serverless web technology
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- Christian Bartram - Initial work - cbartram
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Acknowledgments
- Express for creating a great framework