-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (33 loc) · 1.21 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
const express = require('express');
const app = express();
const port = 3011;
const axios = require('axios');
/*
We’ll tell a route to look for a GET request on the root (/) URL, and return some JSON.
*/
app.get('/', (request, response) => {
response.json({ info: 'Node.js, Express' })
});
app.get('/translate', (request, response) => {
var targetLanguage = request.param('targetLanguage');
var text = request.param('text');
var text2 = request.param.text;
let url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
+ 'auto' + "&tl=" + targetLanguage + "&dt=t&q=" + fixedEncodeURI(text);
axios.get(url)
.then(res => {
response.json({ error: 0, description: "OK", targetLanguage: targetLanguage, textToTranslate: text, result: res.data[0][0][0] });
console.log(res);
})
.catch(error => {
response.json({ error: -1, description: "NOT OK", targetLanguage: targetLanguage, textToTranslate: text });
console.log(error);
});
});
function fixedEncodeURI(str) {
return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
/*Now set the app to listen on the port you set.*/
app.listen(port, () => {
console.log(`App running on port ${port}.`)
})