-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (76 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var express = require("express");
const { translate } = require('bing-translate-api');
var app = express();
const NodeCache = require( "node-cache" ); // prefered node-cache over redis , due to some redis cloud issues
const myCache = new NodeCache();
//const translate = require('google-translate-api'); // Earlier decided to use Google Translatations but was asking for billing :/
const langArray = require('./languages.js');
app.get("/translate/:word/:language", function (req, r) { // GET method with two params : word and target language
var obj = {
text : req.params.word,
toTranslate : req.params.language,
}
var str = JSON.stringify(obj);
if(myCache.has(str)) { // check if the word already exists in cache in memory
console.log('Retrieved value from cache !!')
// Serve response from cache using
// myCache.get(key)
r.json( myCache.get(str))
}else{
// Perform operation, since cache
// doesn't have key
let ar=[];
langArray.forEach(function (lang){
if(lang.includes(req.params.language))
ar=lang;
})
//let ar = langArray[req.params.language];
if(ar!=null)
{ smartCaching(req.params.word,ar);} // smart - pre caching (bonus task)
try{ translate(req.params.word, null, req.params.language, true).then(res => {
myCache.set(str, res.translation)
console.log('Value not present in cache,'
+ ' performing computation')
// r.send("Result: " + res.translation)
r.json(res.translation);
}).catch(err => {
r.json(err);
}
)}
catch(e){
console.log(e);
res.send(500);
}
}
// Set value for same key, in order to
// serve future requests efficiently
}
)
function smartCaching(word,arr)
{
arr.map(x=>{
var obj = {
text : word,
toTranslate : x,
}
var str = JSON.stringify(obj);
try{ translate(word, null, x, true).then(res => {
myCache.set(str, res.translation)
console.log('Value not present in cache,'
+ ' performing computation')
// r.send("Result: " + res.translation)
//r.json(res.translation);
}).catch(err => {
console.log(err);
}
)}
catch(e){
console.log(e);
res.send(500);
}
})
}
var server = app.listen(8080,function(req, res){
console.log("Server running on port 8080");
})
module.exports = server