The application is a Node.js server that exposes REST API to interact with a MongoDB database and implements a cache system layer.
- Node.js version 6.11.12 or higher;
- a MongoDB instance running, version 2.6.10 or higher;
for debian systems:
apt-get install redis-server
sudo redis-server
keep the batch process running in the shell
open a shell go to the project root folder and type the command:
npm install
in the root project folder type:
npm start
- [GET] localhost:3000/data (get all data);
- [GET] localhost:3000/data/<key> (get a value in cache or update data and return a random string);
- [POST] localhost:3000/data , body: {key:<key>, value:<value>} (save or update a key value pair);
- [DELETE] localhost:3000/remove (remove all data);
- [DELETE] localhost:3000/remove/<key> (remove a value for a certain key);
The code is just an example that shows how to implement a cache system with a Node.js server. For a real application consider to:
- implement an ORM such as Mongoose;
- split the config file for different environments (local, development, staging, production);
- split the code in the serve.js entry point putting the routes handlers in a separate file;
- write unit tests;
- configure the cache system;