-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (44 loc) · 1.55 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
const { default: axios } = require('axios');
const express = require('express');
var url = require('url');
// Configuration
const PORT = 3033;
const HOST = "localhost";
const API_SERVICE_URL = "https://swapi.dev/api";
// Create Express Server
const app = express();
app.listen(PORT, HOST, () => {
console.log(`Starting SWAPI Proxy at ${HOST}:${PORT}`);
});
app.use('/', function (req, res) {
var url_parts = url.parse(req.url, true);
var query_str = url_parts.search;
if (query_str === null) query_str = "";
axios({
"method": req.method,
"url": API_SERVICE_URL + req.path + query_str, res
}).then((response) => {
if (req.query["encoding"] == "ewok") {
for (var key in response.data) {
if (Array.isArray(response.data[key])) {
response.data[key] = response.data[key].map(ewokEncode);
} else if (typeof response.data[key] === 'string') {
response.data[key] = ewokEncode(response.data[key]);
}
}
console.log("Response with custom Ewok encoding sent!");
res.send(response.data);
} else {
console.log("Response sent!");
res.send(response.data);
}
}).catch(function (err) {
console.log(err.message + " CURL "+ API_SERVICE_URL + req.path + query_str);
res.send({
"detail": "Not found"
});
});
});
function ewokEncode(text) {
return text.toLowerCase().replace(/[aeiouy]/g, "i").replace(/((?![aeiouy])[a-z])/g, "b");
}