Use Express to Create an API Proxy Server in Node.js
The Node.js framework Express allows us to create web servers and APIs with minimal setup. We will be using Express in a Node.js application to create an API Proxy to request data from another API and return it to a consumer. We can also use Express middleware to help us optimize the API Proxy and improve performance for returning data from the underlying API.
By the end of this tutorial, you should be able to:
- Understand the API Proxy we are creating in Node.js with Express
- Understand the optimizations we will add to our API Proxy
This tutorial is part 1 of 7 tutorials that walk through using Express.js to create an API proxy server. In order to complete the application you’ll need to work through the following tutorials:
- Use Express to Create an API Proxy Server in Node.js (you are here)
- How to Set up an Express.js Server in Node.js
- Organize Your Node.js Code into Modules
- Set up Routes for Your API in Node.js
- Optimize an Express Server in Node.js
- Add Compression to Express in Node.js
- Add Response Caching to a Node.js Express Server
Goal
Understand the API Proxy we are creating in Node.js using Express.
Prerequisites
Overview
In this series of tutorials we are going to use Express to create an API Proxy server.
The goal will be to create an API that makes calls to another API on behalf of consumers. The API Proxy will expose an interface for consumers which is different than the interface of the API the Proxy will be requesting data from.
Express will allow us to very quickly create the API Proxy and focus only on the minimum functionality we need. The rest of this tutorial will walk through an example of what we’ll be creating.
Watch: Use Express to Create an API Proxy Server
What are we building?
We will create an Express server that exposes a simplified API that interacts with the NASA Exoplanet API.
We worked with the Exoplanet API in the ETL tutorials as well. We will reuse code we wrote in that series to make the API calls to the Exoplanet API, as well as code we wrote to transform the planet records from the API.
Our API Proxy will return a subset of data from the Exoplanet API. This allows us to customize the data consumers receive directly to their needs.
Optimizations
We also will introduce caching and compression at the API Proxy level to improve performance of retrieving the data.
Caching stores previously requested data in memory so we can return that data without having to retrieve it from the source each time it is requested. This can decrease the latency for returning data to consumers.
Compression shrinks the size of the payload sent back to the consumer, which can speed up the transfer of the data across the network. We’ll be using gzip compression which can be added to Express with the compression middleware package.
Implementing these optimizations at the API Proxy level is useful because we don’t have control over the Exoplanet API directly. And even if we did, with this approach we don’t have to make any changes to the underlying service itself (the Exoplanet API). We can handle the optimizations at the point of contact for the consumer.
Recap
In this tutorial we discussed the API Proxy we will be creating with Express. We’ll create an API that requests data from the NASA Exoplanet API and transforms it before returning to the client. We’ll add caching and compression to the API Proxy to improve performance for requesting data from the Exoplanet API.
Keep going with the next tutorial in this set: How to Set up an Express.js Server in Node.js.
Additional resources
- Express Documentation (expressjs.com)