-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (55 loc) · 1.71 KB
/
server.js
File metadata and controls
65 lines (55 loc) · 1.71 KB
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
58
59
60
61
62
63
64
65
const express = require("express");
const https = require("https");
const fs = require("fs");
const CSVToJSON = require("csvtojson");
const path = require("path");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
const port = process.env.PORT || 5000;
// const cert = fs.readFileSync("./firemap_global.crt");
// const ca = fs.readFileSync("./firemap_global.ca-bundle");
// const key = fs.readFileSync("./firemap_global.key");
// let options = {
// cert: cert, // fs.readFileSync('./ssl/example.crt');
// ca: ca, // fs.readFileSync('./ssl/example.ca-bundle');
// key: key, // fs.readFileSync('./ssl/example.key');
// };
// also okay: https.createServer({cert, ca, key}, (req, res) => { ...
app.use("/static", express.static("public"));
app.use(express.json());
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, "build")));
let callTime = 25;
function getFireCSV() {
https.get(
"https://firms.modaps.eosdis.nasa.gov/data/active_fire/modis-c6.1/csv/MODIS_C6_1_Global_24h.csv",
(response) => {
const file = fs.createWriteStream("global.csv");
response.pipe(file);
return file;
}
);
}
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.post("/api", (req, res) => {
// Re-caches .csv hourly.
const currTime = new Date().getHours();
if (currTime !== callTime) {
callTime = new Date().getHours();
console.log(`Caching new csv file at ${callTime}`);
getFireCSV();
}
CSVToJSON()
.fromFile("global.csv")
.then((fires) => {
res.json(fires);
})
.catch((err) => {
console.log(err);
});
});
app.listen(port);
console.log(`Server started on port ${port}!`);