-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
71 lines (66 loc) Β· 1.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require("express");
const axios = require("axios");
const http = require("http");
const app = express();
const port = 5000;
let metroInfo = [];
let currentTime = new Date().getHours();
const getSubwayAPI = () => {
// 1 ~ 5μμλ μλ² λλ¦¬μ§ μμμΌλ‘
// api νΈμΆ μλ₯Ό μλ μ μμ. (1μΌ 1000ν μ ν)
// -> κ²°κ΅ κ°±μ μ£ΌκΈ°λ₯Ό μ€μΌ μ μμ!
if (currentTime >= 1 && currentTime <= 5) {
console.log("Server is Dead");
return getSubwayAPI;
}
axios
.get(
`http://swopenAPI.seoul.go.kr/api/subway/79684b6576737061363475626f6853/json/realtimeStationArrival/0/5/${encodeURI(
"νμ "
)}`
)
.then((res) => {
metroInfo = res.data.realtimeArrivalList;
console.log(metroInfo);
})
.catch(function (error) {
console.log(error);
});
return getSubwayAPI;
};
// 20μκ° νλ‘ λλ Έμλ => 72000μ΄
// μ΅λ μμ²κ±΄μ = 1000건
// 72μ΄μ νλ² μμ²νλ©΄ μΌμΆ 1000건 λ§μΌλκΉ μ‘°κΈ μ¬μ μκ² 75μ΄λ§λ€ κ°±μ ν¨
setInterval(getSubwayAPI(), 75000);
startKeepAlive();
app.get("/api/subway", (req, res) => {
res.send(metroInfo);
});
app.listen(port, () => console.log(`server running at port ${port}`));
function startKeepAlive() {
setInterval(function () {
if (currentTime >= 1 && currentTime <= 5) {
console.log("Not working Time");
return;
}
const options = {
host: "hwajeon-station-tracker.herokuapp.com",
port: 80,
path: "/",
};
http
.get(options, function (res) {
res.on("data", function (chunk) {
try {
// optional logging... disable after it's working
console.log("HEROKU RESPONSE: " + chunk);
} catch (err) {
console.log(err.message);
}
});
})
.on("error", function (err) {
console.log("Error: " + err.message);
});
}, 20 * 60 * 1000); // load every 20 minutes
}