Introduction

In this article, we present a Node.js solution for API Gateways implementation. From a developer perspective, we describe how we can easily implement the most common API gateway functionalities using only Node.js and JavaScript ❤️

API Gateway analogy ;) — photo courtesy of https://pexels.com

API Gateway?

An API Gateway encapsulates the internal architecture of your application, by exposing a single, client friendly API through one and only URL; a facade that acts as a single application entry point.

For most distributed applications, it makes sense to implement an API Gateway, which acts as a single entry point into a system. The API Gateway is responsible for request routing, composition, and protocol translation. It provides each of the application’s clients with a custom API. The API Gateway can also mask failures in the backend services by returning cached or default data. https://www.nginx.com/blog/building-microservices-using-an-api-gateway/

Common responsibilities usually placed at API Gateway level

Obfuscation of the internal architecture and distribution of microservices-based applications. Usually reducing configuration overhead.

Non-functional requirements and cross-cutting concerns, making the target services implementation as lightweight and thiner as possible:

— SSL Termination

— Logging

— Application Metrics

— Load Balancing

— Authentication & Authorisation

— Content Negotiation

— Caching***

— Content Compression

— Rates Limit

— …

— SSL Termination — Logging — Application Metrics — Load Balancing — Authentication & Authorisation — Content Negotiation — Caching*** — Content Compression — Rates Limit — … Data aggregation and transformation to simplify clients interaction with the distributed microservices (as usually multiple backend calls are required for the simplest frontend view rendering). Data aggregation also reduces the number of requests/round-trips from the internet.

fast-proxy

Node.js framework agnostic library that enables you to forward an http request to another HTTP server at maximum performance. Supported protocols: HTTP, HTTPS, HTTP2

When we talk about HTTP requests proxying in Node.js, we need to consider the fast-proxy library. It was born using fastify-reply-from as a base, but without all the fastify framework friction and dependencies .

After all performance optimisations, this library can reach up to 46 times the performance of the Node.js widely used http-proxy module:

http-lambda-proxy

Now you can proxy HTTP requests to AWS Lambda functions. 🚀

The http-lambda-proxy module allow us to forward HTTP requests to services running inside AWS Lambda functions. This feature enable many awesome enterprise features:

Very low cost for computing instances

Unprecedented scalability with rather constant execution times

… and many more

Forwarding HTTP requests on port 8080 to AWS Lambda function

Also, by using the great serverless-http module, developers can run full-size REST APIs as lambdas…, let me say this again:

You can safely run your stateless Node.js REST APIs inside AWS Lambda functions!

Meet fast-gateway

“fast-proxy” and “http-lambda-proxy” libraries are too low level, let’s provide a friendly, all in one API Gateway solution for HTTP and Serverless based distributed applications: