-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
52 lines (45 loc) · 1.58 KB
/
server.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
///////////////////////////////////////////////////////////////////////
// CSV Web Data Connector //
// A Tableau Web Data Connector for connecting to hosted CSVs. //
// Author: Keshia Rose //
// GitHub: https://github.com/KeshiaRose/Basic-CSV-WDC //
// Version 1.0 //
///////////////////////////////////////////////////////////////////////
const express = require("express");
const fetch = require("node-fetch");
const iconv = require("iconv-lite");
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
app.post("/proxy/*", async (req, res) => {
let url = req.url.split("/proxy/")[1];
let options = {
method: req.body.method
};
if (req.body.token) {
options["headers"] = {
Authorization: `Bearer ${req.body.token}`
};
}
try {
let data;
let response = await fetch(url, options);
if (req.body.encoding && req.body.encoding !== "") {
let buffer = await response.arrayBuffer();
data = iconv.decode(new Buffer(buffer), req.body.encoding).toString();
} else {
data = await response.text();
}
response.ok ? res.send(data) : res.send({ error: response.statusText });
return;
} catch (error) {
res.send({ error: error.message });
return;
}
});
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});