days
0
-8
hours
-2
-3
minutes
0
0
seconds
-1
-5
search
Because we never have enough web frameworks

Lambda API: A lightweight web framework that will rock your serverless world

Eirini-Eleni Papadopoulou
lambda
© Shutterstock / pathdoc

The serverless universe is expanding once again and this time its welcomes Lambda API, a lightweight web framework for use with AWS API Gateway and AWS Lambda using Lambda Proxy Integration.

With more tools and technologies joining in, the serverless universe constantly expands!

Today we present you Lambda API, a lightweight web framework for use with AWS API Gateway and AWS Lambda using Lambda Proxy Integration created by serverless advocate Jeremy Daly.

Although it is based on and mirrors other web frameworks like Express.js and Fastify, Lambda API is quite unique in one key aspect. It has zero dependencies!

When it comes to serverless applications that need to load quickly, all these extra dependencies that you have to use with your additional Node.js modules slow down execution and use more memory than necessary. But Lambda API is here to change that!

As fast as The Flash?

Lambda API was built to be lightweight and fast. And it looks like it’s on the right track! Let’s have a closer look at its main features:

  • Zero dependencies. Yup, still not a joke!
  • Super lightweight and built specifically for serverless applications using AWS Lambda and API Gateway
  • Provides support for API routing, serving up HTML pages, issuing redirects, serving binary files and more
  • Offers a built-in logging engine that can even periodically sample requests for things like tracing and benchmarking
  • It has a powerful middleware and error handling system
  • Designed to work with Lambda’s Proxy Integration, automatically handling all the interaction with API Gateway for you
  • It parses REQUESTS and formats RESPONSES, allowing you to focus on your application’s core functionality
  • Create just one function that handled all your user management features

If you are still not convinced, let’s take a closer look at some of the framework’s functions.

Routes and HTTP Methods Routes are defined by using convenience methods or the METHOD method. There are currently eight convenience route methods: get()post()put()patch()delete()head()options() and any().

Returning Responses Supports both callback-style and async-await for returning responses to users. The RESPONSE object has several callbacks that will trigger a response (send()json()html(), etc.)

Route Prefixing Easy to create multiple versions of the same api without changing routes by hand. The register()method allows you to load routes from an external file and prefix all of those routes using the prefix option.

Debugging Routes Offers a routes() method that can be called on the main instance that will return an array containing the METHODand full PATH of every configured route. This will include base paths and prefixed routes.

REQUEST – The REQUEST object contains a parsed and normalized request from API Gateway. It contains a number values by default. Check out the full list here.

RESPONSE  The RESPONSE object is used to send a response back to the API Gateway. The RESPONSE object contains several methods to manipulate responses. All methods are chainable unless they trigger a response. You can find the list here.

SEE ALSO: Architect simplifies Lambda functions for the cloud

Logging Includes a robust logging engine specifically designed to utilize native JSON support for CloudWatch Logs. Not only is it super fast, but it’s also highly configurable.

Middleware It supports middleware to preprocess requests before they execute their matching routes. Middleware is defined using the use method and requires a function with three parameters for the REQUESTRESPONSE, and next callback.

Clean Up It has a built-in clean up method called ‘finally()’ that will execute after all middleware and routes have been completed, but before execution is complete.

Error Handling Automatically catches and logs errors using the Logging system.

Namespaces Allows you to map specific modules to namespaces that can be accessed from the REQUEST object.

Lambda Proxy Integration That’s an option in API Gateway that allows the details of an API request to be passed as the eventparameter of a Lambda function.

If you are interested in a full walk-through on how to build a serverless API with serverless, AWS Lambda and Lambda API, check out Jeremy Daly’s tutorial.

Getting started

To install Lambda API simply run

npm i lambda-api --save

Keep in mind, however, that you will need AWS Lambda running Node 8.10  and AWS API Gateway using Proxy Integration.

Author
Eirini-Eleni Papadopoulou
Eirini-Eleni Papadopoulou is an assistant editor for JAXenter.com. Just finished her masters in Modern East Asian Studies and plans to continue with her old hobby that is computer science.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of