main.js
2.83 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
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 url='https://api.edamam.com/api/food-database/parser';
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()
}))
app.use(express.static('data'));
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.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.get('/logout', function(request, response) {
request.session.destroy(function(err){
response.redirect('/');
})
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!')
});