This is a simple mavel characters api.
Install Node.js on your machine.
-
Install dependencies
npm install
-
copy /.env.example to /.env and update the .env file with your Marvel's api keys (MARVEL_PUBLIC_KEY & MARVEL_PRIVATE_KEY). you can get the Marvel's api keys from Marvel's Developers Site. The default port is set to 8080 but you can change it in the .env file
PORT=8080 MARVEL_PUBLIC_KEY=your-public-key MARVEL_PRIVATE_KEY=your-private-key
-
Build the project
npm run build
-
Run the app
npm run start
Access the api docs on http://localhost:8080/docs
To run the tests
npm run test
In this project node-cache library was used to do an in-momory caching. Refer to character.service.ts file to see the code. Below are the steps taken to cache the results obtained from Marvel's API:
- Get the last modified character & total number of characters in Marvels API using /characters endpoint with (orderBy: '-modified' and limit:1).
- Compare the last_modified timestamp obtained in step 1 with the cached last_modified value.
- If both values are equal, then serve the data from the cache
- If not equal, then get the latest list from Marvel’s API then store the results in the cache and update the cached last_modified.
After we get the total number of records (poinit 1 above), we create an array of javascript promises. Each promise will have a limit of 100 records. So if the total number of characters on Marvel’s API is 1450, 15 promises will be created and run concurrently to get the data. After getting all the data we will filter the ids only cache then in memory.