Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cbartram/lamb-chop
Write serverless routes like traditional server sided code.
https://github.com/cbartram/lamb-chop
api aws express gateway lambda router routes serverless syntax
Last synced: about 4 hours ago
JSON representation
Write serverless routes like traditional server sided code.
- Host: GitHub
- URL: https://github.com/cbartram/lamb-chop
- Owner: cbartram
- License: apache-2.0
- Created: 2020-02-02T03:23:22.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:30:29.000Z (almost 2 years ago)
- Last Synced: 2024-04-15T04:09:27.175Z (7 months ago)
- Topics: api, aws, express, gateway, lambda, router, routes, serverless, syntax
- Language: JavaScript
- Homepage:
- Size: 462 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-http.html) 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:
```javascript
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:
```javascript
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.
```javascript
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.
```javascript
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:
```javascript
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](https://nodejs.org/en/) - The web framework used
* [NPM](https://www.npmjs.com/) - Dependency Management
* [Javascript](https://www.javascript.com/) - Programming language used
* [Lambda](https://aws.amazon.com/lambda/) - Serverless web technology## Contributing
Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us.
## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags).
## Authors
* **Christian Bartram** - *Initial work* - [cbartram](https://github.com/cbartram)
See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project.
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
## Acknowledgments
* Express for creating a great framework