main.js
6.03 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var express = require('express')
var app = express()
var fs = require('fs');
var bodyParser = require('body-parser');
var helmet = require('helmet')
app.use(helmet());
var session = require('express-session');
var FileStore= require('session-file-store')(session);
var template = require('./lib/template.js');
var container = require('./lib/container.js');
var axios = require('axios');
var request = require("request");
var low =require('lowdb')
var FileSync = require('lowdb/adapters/FileSync');
var adapter = new FileSync('db.json');
var db=low(adapter);
db.defaults({users:[]}).write();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(session({
secret: 'asadlfkj!@#!@#dfgasdg',
resave: false,
saveUninitialized: true,
store: new FileStore()
}))
var app_id = "a59b8963";
var api_key = "1c4c66e13fa8bf8d4d5c5a6c8ba1cf5b";
var foodkind;
var ingr;
var sID;
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const OpenWeatherMapHelper = require("openweathermap-node");
const helper = new OpenWeatherMapHelper({
APPID: 'afea3779ccf698668f89764635ac580f',
units:"imperial"
})
app.use(express.static('data/images'));
app.get('/', function(request, response) {
fs.readFile('./contents/main.html', function(error, body){
var title = 'Health Care';
var ch='15ch'
var html = template.HTML(title,ch,'',body,'','');
response.send(html);
});
});
app.post('/login_process', function(request, response) {
var post = request.body;
var ID=post.ID;
var password=post.pwd;
var user = db.get('users').find({ID:ID,password:password}).value();
if(user){
request.session.is_logined = true;
request.session.ID=ID;
sID=ID;
request.session.save(function(){
response.redirect('/InputPage')
});
}
});
app.get('/signup', function(request, response) {
fs.readFile('./contents/signup.html', function(error, body){
var title = 'Sign Up Page';
var ch='5ch'
var html = template.HTML(title,ch,'<link rel="stylesheet" type="text/css" href="./a.css" />',body,'','');
response.send(html);
});
});
app.post('/signup_process', function(request, response) {
var post = request.body;
var ID=post.ID;
var pwd=post.pwd;
var age=post.age;
var gender=post.gender;
var height=post.height;
var weight=post.weight;
db.get('users').push({
ID:ID,
password:pwd,
age:age,
gender:gender,
height:height,
weight:weight
}).write();
request.session.is_logined = true;
request.session.ID = ID;
request.session.save(function(){
response.redirect('/InputPage')
});
});
app.get('/InputPage', function(request, response){
fs.readFile('./contents/Input.html', function(error, body){
var title = 'Input Page';
var ch='5ch'
var html = template.HTML(title,ch,``,body,`#logout{
position:absolute;
right: 20ch;
}`,'');
response.send(html);
});
});
app.post('/foods', function(request, response){
foodkind=request.body;
ingr=foodkind.name;
response.redirect('/ingr');
});
app.get('/ingr',function(req, res){
var city;
var temper;
var weather;
var detweather;
var wind;
var cloud;
helper.getCurrentWeatherByCityName("Seoul", (err, currentWeather) => {
if(err){
console.log(err);
}
else{
console.log(currentWeather);
}
city=currentWeather.name;
temper=currentWeather.main.temp;
weather=currentWeather.weather[0].main;
detweather=currentWeather.weather[0].description;
wind=currentWeather.wind.speed;
cloud=currentWeather.clouds.all;
});
request(
{ method: 'GET'
, uri: 'https://api.edamam.com/api/nutrition-data?app_id=' + app_id + '&app_key=' + api_key + '&ingr=' + ingr
, gzip: true
}
, function (error, response, result) {
fs.readFile('./contents/Result.html', function(error, body){
var title = 'Result Page';
var ch='5ch'
var jresult=JSON.parse(result);
var cal = jresult.calories;
var user = db.get('users').find({ID:sID}).value();
var bmi = user.weight/(user.height*user.height/100/100);
var ccc = container.L(`<h2>칼로리 : ${cal}kcal`,`<p style="margin-left: 10%">"Weather NOW"</p>
<p id="w1" style="margin-left: 10%">도시 = ${city}</p>
<p id="w3" style="margin-left: 10%">현재온도 = ${temper}(+" ℃")</p>
<span id="w2" style="margin-left: 10%">현재날씨 = ${weather}</span>
<span id="w4" style="margin-left: 10%">날씨상세 = ${detweather}</span>
<br><br>
<span id="w5" style="margin-left: 10%">바람 = ${wind}(+"m/s")</span>
<span id="w6" style="margin-left: 10%">구름 = ${cloud} (+"%")</span>`,`<h2>bmi: ${bmi}</h2>`)
var html = template.HTML(title,ch,``,body,`#logout{
position:absolute;
right: 20ch;
}
#section{
position: absolute;
margin-left:5%;
float:left;
width: 40%;
height: 90%;
}
#container1{
float: left;
border: 1px solid white;
width: 100%;
height: 90%;
}
#container2{
float: left;
border: 1px solid white;
background-color: rgb(102, 219, 180);
width: 100%;
height: 120%;
}
#container3{
float: right;
border: 1px solid white;
margin-right:5%;
width: 40%;
height: 90%;
}`,ccc);
res.send(html);
});
});
});
app.get('/logout', function(request, response) {
request.session.destroy(function(err){
response.redirect('/');
})
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!')
});