-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
57 lines (48 loc) · 1.45 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
53
54
55
56
57
import express from "express";
import morgan from "morgan";
import axios from "axios";
import path from "path";
import { fileURLToPath } from "url";
const app = express();
const port = 8080;
// Get the current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Log all requested URLs to the console.
app.use(morgan("dev"));
// Serve static files from soundscape-web-client so we can moke the real server
app.get("/", function (req, res) {
res.redirect("/soundscape-web-client");
});
app.use(
"/soundscape-web-client",
express.static(path.join(__dirname, "dist"))
);
// Proxy handler
const proxyHandler = async (req, res) => {
try {
const targetUrl = `https://tiles.soundscape.services${req.originalUrl}`;
const response = await axios.get(targetUrl, {
headers: {
"User-Agent": "soundscape-web-client/0.1 (dev server)",
},
responseType: "arraybuffer",
});
res.set({
...response.headers,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
});
res.status(response.status).send(response.data);
} catch (error) {
res.status(500).send("Proxy error");
}
};
// Route for /tiles
app.use("/tiles/*", proxyHandler);
app.listen(port, () => {
console.log(
`Server is running on http://localhost:${port}/soundscape-web-client/`
);
});