lambda-expressless
v1.7.2
Published
Wrap AWS Lambda functions with Express-like functions to simplify your code
Downloads
14
Readme
lambda-expressless
Wrap AWS Lambda functions with Express-like functions to simplify your code
So instead of writing this:
exports.handler = (event, context, callback) => {
const requestBody = JSON.parse(event.body);
const responseBody = {
success: false,
data: requestBody.id
};
callback(null, {
statusCode: 201,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(responseBody)
});
}
you'll have this:
const { use } = require('lambda-expressless');
const bodyParser = require('body-parser');
exports.handler = use(bodyParser.json(), (req, res) => {
res.status(201).json({
success: false,
data: req.body.id
})
});
You can also use multiple middlewares for a single handler:
const { use } = require('lambda-expressless');
const checkUser = (req, res, next) => {
if (req.get('Authorization') === 'someToken') {
next()
} else {
req.status(403).end('Forbidden');
}
};
const getUser = (req, res) => {
res.json({
id: '12',
name: 'Murat'
});
};
exports.handler = use(checkUser, getUser);
TypeScript example:
import { use, Request, Response } from 'lambda-expressless';
import * as bodyParser from "body-parser";
const addUser = (req: Request, res: Response, next: Function) => {
UserService.add(req.body);
// add user
res.json({success: true});
};
export const handler = use(
bodyParser.json(),
addUser
);
You can use many popular Express Middlewares. Some tested examples are:
Installation
npm i lambda-expressless
Supported Features and Limitations
This project aims to implement functionalities of ExpressJS middlewares as much as possible. Request
and Response
objects have properties and methods listed below.
Request Object
Properties:
| Property | Notes | |-------------|-------| | body | You need to use body-parser | | hostname | - | | host | - | | xhr | - | | ip | - | | ips | - | | path | - | | protocol | - | | secure | - | | method | - | | query | - | | params | - | | headers | - |
Methods:
| Method | Notes | |-------------|-------| | accepts() | - | | acceptsEncodings() | - | | acceptsCharsets() | - | | acceptsLanguages() | - | | get() | - | | header() | - | | is() | - |
Response Object
Methods:
| Method | Notes |
|-------------|-------|
| get() | - |
| format() | Doesn't support shorthand mime-types |
| set() | Only supports key, value
parameters |
| send() | Only supports string values |
| status() | - |
| end() | - |
| json() | - |
| type() | - |
Contribution
Every contribution is very welcome. Keep these in your mind when you want to make a contribution:
- Because of we use semantic-release you need to use Angular Commit Message Conventions in your commit messages.
- Keep code coverage 100% with your updated tests.
- Check your changes with a Lambda environment. You can use SAM-CLI to test on your local.
- Don't forget to update documentation(this readme file) about your changes.