-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server.js
191 lines (163 loc) · 4.69 KB
/
test_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const fs = require('fs');
const path = require('path');
const express = require('express'); // require()로 express module 불러옴.
const app = express(); // app 객체에 담음
const port = 8807;
// 1. 현재 시간(Locale)
app.get('/', (req, res)=>{
res.send("Starbucks with Deung.");
console.log("test console.log");
// res.end('Hello World!?? San?asdfsad');
});
app.post('/meals/kor', (req, res) => {
// app.get('/meals/kor', (req, res) => {
const curr = new Date();
const utc = curr.getTime() + (curr.getTimezoneOffset() * 60 * 1000);
const KR_TIME_DIFF = 9 * 60 * 60 * 1000;
const kr_curr = new Date(utc + KR_TIME_DIFF);
const m = kr_curr.getMonth() + 1;
const d = kr_curr.getDate();
const h = kr_curr.getHours();
console.log("now hour : " + h);
let month = m;
let date = d;
let meal = '';
let days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let name = '';
if (Boolean(9 <= h && h < 13)) {
meal = 'l';
} else if (Boolean(13 <= h && h < 19)) {
meal = 'd';
} else {
meal = 'b';
date = d + 1;
if (Boolean(date > days[m-1])) {
month = m + 1;
date = 1;
if (Boolean(month > 12)) {
month = 1;
}
}
}
month = String(month);
date = String(date);
if (Boolean(month.length == 1)) {
month = month.padStart(2, '0');
}
if (Boolean(date.length == 1)) {
date = date.padStart(2, '0');
}
name = month + '_' + date + '_' + meal + '_kor.json';
console.log(name);
const Dir = path.join(__dirname, 'meals_data', name);
var isExist = 1;
try {
fs.statSync(Dir);
} catch (error) {
if (error.code === "ENOENT") {
isExist = 0;
console.log("파일이 존재하지 않습니다.");
}
}
var content = '';
var time_content = '';
// 17~19 이런거 추가
if(isExist) {
const file = fs.readFileSync(Dir, 'utf8');
const jsonData = JSON.parse(file);
const jsonMeals = jsonData.meal;
content = (jsonMeals.meal_date + " " + jsonMeals.kind_of_meal + "\n\n" +
jsonMeals.title + "\n\n" + jsonMeals.menu);
} else {
}
const responseBody = {
version: "2.0",
template: {
outputs: [
{
simpleText: {
text: content
}
}
]
}
};
res.status(200).send(responseBody);
});
app.post('/meals/eng', (req, res) => {
const curr = new Date();
const utc = curr.getTime() + (curr.getTimezoneOffset() * 60 * 1000);
const KR_TIME_DIFF = 9 * 60 * 60 * 1000;
const kr_curr = new Date(utc + KR_TIME_DIFF);
const m = kr_curr.getMonth() + 1;
const d = kr_curr.getDate();
const h = kr_curr.getHours();
let month = m;
let date = d;
let meal = '';
let days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let name = '';
if (Boolean(9 <= h && h < 13)) {
meal = 'l';
} else if (Boolean(13 <= h && h < 19)) {
meal = 'd';
} else {
meal = 'b';
date = d + 1;
if (Boolean(date > days[m-1])) {
month = m + 1;
date = 1;
if (Boolean(month > 12)) {
month = 1;
}
}
}
month = String(month);
date = String(date);
if (Boolean(month.length == 1)) {
month = month.padStart(2, '0');
}
if (Boolean(date.length == 1)) {
date = date.padStart(2, '0');
}
name = month + '_' + date + '_' + meal + '_eng.json';
console.log(name);
const Dir = path.join(__dirname, 'meals_data', name);
var isExist = 1;
try {
fs.statSync(Dir);
} catch (error) {
if (error.code === "ENOENT") {
isExist = 0;
console.log("파일이 존재하지 않습니다.");
}
}
var content = '';
var time_content = '';
// 17~19 이런거 추가
if(isExist) {
const file = fs.readFileSync(Dir, 'utf8');
const jsonData = JSON.parse(file);
const jsonMeals = jsonData.meal;
content = (jsonMeals.meal_date + " " + jsonMeals.kind_of_meal + "\n\n" +
jsonMeals.title + "\n\n" + jsonMeals.menu);
} else {
}
const responseBody = {
version: "2.0",
template: {
outputs: [
{
simpleText: {
text: content
}
}
]
}
};
res.status(200).send(responseBody);
});
// listen : localhost:3000과 연결해줌
app.listen(port, ()=>{
console.log('listen = http://localhost:%s', port);
});