NodeJS proxy api for iTunes
By: Sandeep Kumar
Tags: iTunes, Proxy Api, Rest Api, Expressjs, Nodejs

Step 1: Create Project Folder

mkdir nodejs-proxy-api-itunes && cd nodejs-proxy-api-itunes

Step 2: Initialize node project

Initialize node project using init command.After this, you will see a series of questions and select the default option for now.

npm init

Step 3: Install required npm packages

npm install express body-parser dotenv helmet morgan cors axios

Please search here for detailed explanation for each of the packages.

Step 4: Create project folder structure

At this point, you can create the below mention folder structure for easier reference to this article.

Step 5: Understanding the application flow

index.js is entry point of the application and along with app.js is responsible for creating express app, does configuration on app instance, makes app to listen for request on specific port and configures middleware for the application with middlewares.js

Index.js

App.js

middlewares.js

src/api: This folder will have the code for the different APIs

src/api/index.js: This is the entry point of the api, requests will be routed to different handlers from this file.

router.use('/itunes', itunesGames)

The above code basically tells the router that if request url contains ‘/itunes’, redirect the request to api handler inside src/api/itunesgames.js

Now let’s look at the src/api/itunesgames.js. This is responsible to make the public API call in this case (iTunes API) to fetch the new games from iTunes store and along with the responsible to cache, rate limit and delay the api call for repeated requests.

This is how you set up a express proxy which will call the iTunes API to fetch data and lets break into the code :)

Here, i have added 2 npm packages called express-rate-limit and express-slow-down

npm install express-rate-limit express-slow-down

express-rate-limit is a rate limiting middleware for express and used to limit repeated requests to public APIs.

const limiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 100 // limit each IP to 100 requests per windowMs });

express-slow-down is a Basic rate-limiting middleware for Express that slows down responses rather than blocking them outright. Use to limit repeated requests to public APIs.

const speedLimiter = slowDown({

windowMs: 15 * 60 * 1000, // 15 minutes

delayAfter: 100, // allow 100 requests per 15 minutes, then...

delayMs: 500 // begin adding 500ms of delay per request above 100:

// request # 101 is delayed by 500ms

// request # 102 is delayed by 1000ms

// request # 103 is delayed by 1500ms

// etc.

});

iTunes API

We can use iTunes API to fetch various data such as iTunes music, IOS apps and various feeds such as “Top free apps”, “Top paid apps” and many more..

In this article, i will be using iTunes RSS feed generator to get the end points for this example and you can refer the link here

Also, if you are interested in looking at apple music api, please refer the following link here

Here is the endpoint to fetch top 25 IOS apps “New Games we love” from iTunes feed generator and we will using this in the example

API end point url: https://rss.itunes.apple.com/api/v1/us/ios-apps/new-games-we-love/all/25/explicit.json

In the above, i am using memory to cache the data based on cacheTime for anything that is repeatedly called in 60 seconds. This is basic way of caching data, however for mid to large size projects, we can use better mechanisms to cache the data. I will publish the process in another article :)

In the process, i am adding cacheTime property to my response data object. This is also one of the benefits of having proxy API where we need to add, modify any data attributes for our application to work.

In the next step, it uses axios to make a call to the iTunes API and send response back on the proxy API along with added data property i. e cacheTime.

Things to look for:

Hope you liked this article on setting up nodejs proxy API, caching data, rate limiting, slow down response for repeated requests and process to add new data property to your response object to support your application need.

Watch out for more articles in next couple of weeks from me on: