Skip to content

Use ExpressJS Middlewares as AWS Lambda handlers

License

Notifications You must be signed in to change notification settings

muratcorlu/lambda-expressless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lambda-expressless

Build Status npm version codecov semantic-release

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:

  1. Because of we use semantic-release you need to use Angular Commit Message Conventions in your commit messages.
  2. Keep code coverage 100% with your updated tests.
  3. Check your changes with a Lambda environment. You can use SAM-CLI to test on your local.
  4. Don't forget to update documentation(this readme file) about your changes.

About

Use ExpressJS Middlewares as AWS Lambda handlers

Resources

License

Stars

Watchers

Forks

Packages

No packages published