-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (36 loc) · 1.2 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
const express = require("express");
const path = require("path");
const { createProxyMiddleware } = require("http-proxy-middleware");
const fetch = require("node-fetch").default;
const app = express();
const port = process.env.PORT || 3000;
// Serve static files from the React app
app.use(express.static(path.join(__dirname, "client/build")));
// Proxy API requests to the WalkScore API
app.use("/score", async (req, res) => {
const { lat, lng, address } = req.query;
const apiKey = process.env.REACT_APP_WALKSCORE_KEY;
const url = `https://api.walkscore.com/score?format=json&lat=${lat}&lon=${lng}&address=${address}&wsapikey=${apiKey}`;
try {
const response = await fetch(url);
const json = await response.json();
res.json(json);
} catch (error) {
console.error(error);
res.status(500).send("Error retrieving Walk Score data");
}
});
// Handle all other requests by returning the React app
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build/index.html"));
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
import("node-fetch")
.then((fetch) => {
// use fetch here
})
.catch((err) => {
// handle error here
});