-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (133 loc) · 5.18 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//index.js
const http = require("http");
const AppData = require("./controller");
const PORT = process.env.PORT || 5015;
const server = http.createServer(async (req, res) => {
// / : GET
if (req.url === "/" && req.method === "GET") {
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*" });
res.end(JSON.stringify({ message: "App is active! 🚀" }));
}
// /api/v1 : GET
else if (req.url === "/v1/fulldata" && req.method === "GET") {
// get the data.
const allData = await new AppData().getAllData();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(allData));
}
//
//
// /api/v1/today : GET
else if (req.url === "/v1/today" &&
req.method === "GET") {
// get today data.
const todayData = await new AppData().getTodayData();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(todayData));
}
//
/*
// /api/v1/yesterday : GET
else if (req.url === "/v1/yesterday" &&
req.method === "GET") {
// get yesterday data.
const yesterdayData = await new AppData().getYesterdayData();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(yesterdayData));
}
//
// /api/v1/tomorrow : GET
else if (req.url === "/v1/tomorrow" &&
req.method === "GET") {
// get tomorrow data.
const tomorrowData = await new AppData().getTomorrowData();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(tomorrowData));
}
//
// /api/v1/holiday : GET
else if (req.url === "/v1/holiday" &&
req.method === "GET") {
// get holiday data.
const holidayData = await new AppData().getHolidayData();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(holidayData));
}
//
//
// /v1 : GET 7 days Data
else if (req.url === "/v1/7days" && req.method === "GET") {
// get legal Holiday.
const dataNext7days = await new AppData().get7Data();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(dataNext7days));
}
//
// /v1 : GET Year Data
else if (req.url === "/v1/year" && req.method === "GET") {
// get year data.
const dataFullYear = await new AppData().getYearData();
// set the status code, and content-type
res.writeHead(200, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(dataFullYear));
}
//
// /v1/date/:MMDDYY : GET
else if (req.url.match(/\/v1\/date\/([0-9]+)/) &&
req.method === "GET") {
try {
// get date 10272022 from url
const X = req.url.split("/")[3];
// get a single data
const singleData = await new AppData().getSingleData(X);
// set the status code and content-type
res.writeHead(200, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the data
res.end(JSON.stringify(singleData));
} catch (error) {
// set the status code and content-type
res.writeHead(404, { "Content-Type": "application/json",
"Access-Control-Allow-Origin": "*" });
// send the error
res.end(JSON.stringify({ message: error }));
}
}
*/
// Add above
// No route present
else {
res.writeHead(404, { "Content-Type": "application/json; charset=utf-8",
"Access-Control-Allow-Origin": "*" });
res.end(JSON.stringify({ message: "Route not found 💣" }));
}
});
server.listen(PORT, () => {
console.log(`server started on port: ${PORT}`);
});
// I will add credit or inspiration later.
// Avoid a single error from crashing the server in production.
process.on("uncaughtException", (...args) => console.error(args));
process.on("unhandledRejection", (...args) => console.error(args));