Server.js
In this file we will find all the code used to create the Node.js server, and the code used to connect to Redis.
Headings
In this code section we will require all the used libraries:
- Request, that will handle the petitions to the remote API
- Ioredis, handling the requests to the Redis server
- Http, to initialize the Node Server
const request = require('request');
const Redis = require('ioredis');
const http = require('http');
Creating the servers
In this section we will initialize the Node Server in the 8000 port, calling the "executeRequest" function when some conection is recieved, and conect our code to the Redis Server, previously initialized in the 6379 port, or in Docker
const redis = new Redis(6379, '127.0.0.1');
http.createServer(executeRequest).listen(8000);
ExecuteRequest
This is the main function of our code, the one that handles all the petitions, it will have two parameters: call and answer.
Call containing all the information from the petition to our Node server, and answer containing the response.
executeRequest = (call, answer) => {
// All the code goes here
}
First thing to do is get the URL from where the client is requesting information (the API url).
let Url = call.url;
Url = Url.substr(1, Url.length);
Then we need to initialize a promise, that will handle the petition to the server, either the Redis' one or to the remote API, when this promise is resolved, we'll need to return the response
new Promise((resolve, reject)=>{
// Connection to the servers
}).then((response)=>{
answer.setHeader("Access-Control-Allow-Origin", "*");
answer.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST");
answer.writeHead(200, {'Content-Type': 'text/plain'});
answer.write(response);
answer.end();
});
Once inside the Promise, first thing to do is checking either the value is on Redis, or if it has to be requested from an external API
let value;
redis.get(Url, (err, result) => {
if(!err && result){
// Code if the value is on redis (result will contain the requested value)
} else {
// Code if the value has to be requested from an external API
}
});
If the value is on redis, we simply have to return it (adding a Redis procedence, so the client is able to know the procedence of the value).
value = {
info: result,
from: "Redis",
};
resolve(JSON.stringify(value));
Else, if the value ain't on Redis, it has to be requested from the external API
const options = {
url: Url,
headers: {
'User-Agent': 'request',
}
};
request(options, (error, response, body)=>{
// Code to execute when the value is recieved from the external API
});
When the value from the external API is recieved, it has to be stored on redis for future consults and it is returned (adding an External API procedence, so the client is able to know the procedence of the value).
if (!error && response.statusCode == 200) {
const info = JSON.stringify(body);
redis.set(Url, info); //Stores the value in Redis
value = info;
}
value = {
info: value,
from: "Remote Api",
};
resolve(JSON.stringify(value));
Complete Code
const request = require('request');
const Redis = require('ioredis');
const http = require('http');
const redis = new Redis(6379, '127.0.0.1');
executeRequest = (call, answer) => {
let Url = call.url;
Url = Url.substr(1, Url.length);
new Promise((resolve, reject)=>{
let value;
redis.get(Url, (err, result) => {
if(!err && result){
value = {
info: result,
from: "Redis",
};
resolve(JSON.stringify(value));
} else {
const options = {
url: Url,
headers: {
'User-Agent': 'request',
}
};
request(options, (error, response, body)=>{
if (!error && response.statusCode == 200) {
const info = JSON.stringify(body);
redis.set(Url, info);
value = info;
}
value = {
info: value,
from: "Remote Api",
};
resolve(JSON.stringify(value));
});
}
});
}).then((response)=>{
answer.setHeader("Access-Control-Allow-Origin", "*");
answer.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST");
answer.writeHead(200, {'Content-Type': 'text/plain'});
answer.write(response);
answer.end();
});
};
http.createServer(executeRequest).listen(8000);