-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (101 loc) · 3.66 KB
/
app.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
const mysql = require ('mysql');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const session = require('express-session');
const mailer = require('nodemailer');
const userController = require('./userController.js');
const adminController = require('./adminController.js');
const dogController = require('./dogController.js');
const bookingController = require('./bookingController.js');
const app = express();
const portNum = process.env.PORT || 3000;
/*
http.createServer(function (req, res) {
fs.readFile('signup.html', function(err, data) {
res.writeHead(200, {'Content-type': 'text/html'});
res.write(data);
res.end();
})
}).listen(8000);
console.log('Server started on port '+8000);
*/
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: false
}))
//edit the following fields according to your database setup
const db = mysql.createConnection({
host: 'hostname',
user: 'username',
password: 'password',
port: 'portnumber',
database: 'databasename'
});
db.connect((err) => {
if(err){
throw err;
}
console.log('mysql connected...');
});
//signup and login page route
app.get('/',function(req, res){
res.render('signup',{
error: null
});
});
app.get('/signup',function(req, res){
res.render('signup',{
error: null
});
});
app.get('/login', (req,res) => {
res.render('login',{
error: null
});
})
let router = express.Router();
// the get function expect a callback with signature(req,res,next), so the database variable db
// is passed to the function in the Controller, the corresponding function will do something with the db variable,
// and return the callback function with the signature expected.
// JavaScript has first-class functions, being able to pass function as argument to other functions,
// return functions as the values from other functions, assign them to variables or data structure.
// https://en.wikipedia.org/wiki/First-class_function
//user page route
router.post('/signup',userController.addUser(db));
router.post('/login',userController.login(db));
router.get('/user/:id',requiresLogin, userController.getUserPage(db));
router.post('/user/edit/:id', userController.editUser(db));
//admin page route
router.get('/admin',adminController.getAdmin(db));
//dog panel route
router.post('/dogs/add/:userid', dogController.addDog(db));
router.post('/dogs/edit/:userid/:dogsid', dogController.editDog(db));
router.post('/dogs/delete/:userid/:dogsid', dogController.deleteDog(db));
//booking panel route
router.post('/appointment/add', bookingController.addBooking(db,false)); //enable booking confirm email by setting second argument to true
router.post('/booking/edit/:userid/:bid', bookingController.editBooking(db));
router.post('/booking/delete/:userid/:bid', bookingController.deleteBooking(db));
app.use(router);
//the following function which tries to setup session for user login doesn't work properly
function requiresLogin(req, res, next){
if (req.session && req.session.userId){
return next();
}else{
var err = new Error('You must be logged in to view this page.');
err.status = 401;
res.redirect('/login');
}
}
app.listen(portNum, function(){
console.log('Server started on port '+portNum);
})
//the following line tries to setup automatic reminder email every 24 hours
//autoEmail();
//setInterval(autoEmail,86400000);