-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (91 loc) · 3.69 KB
/
index.js
File metadata and controls
106 lines (91 loc) · 3.69 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const dbHandler = require('./include/Db_Handler.js');
//define the port used by the application (process.env.PORT is needed for environment like herokuapp)
const port = process.env.PORT || 8080;
//Allow the application to parse json file and read encoded url
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//Add debug information on the console
//app.use(require('morgan')('combined')); //useful value :dev, combined
//Used for authenticate system.
require('./auth.js')(app);
require('./rating.js')(app);
app.use(express.static('public'));
//Used for static ejs files
app.set('views', __dirname + '/views/pages');
app.set('view engine', 'ejs');
//homepage
app.get('/', function (req, res) {
res.render('index', {auth: req.isAuthenticated(), page_name: "home"});
});
//about us poge
app.get('/about', function (req, res) {
res.render('about', {auth: req.isAuthenticated(), page_name: "about"});
});
//users account page
app.get('/account', function (req, res) {
res.render('account', {auth: req.isAuthenticated(), page_name: "account"});
});
//bar page where users can view saved info
app.get('/bar', function (req, res) {
const email = req.session.email;
dbHandler.getUserByEmail(email, function (err, user) {
if (err) return res.render('bar', {auth: req.isAuthenticated(), page_name: "myBars", bars: null});
if (user) {
dbHandler.getAllBarRatedByUser(user.uuid, function (err, bars) {
if (err) {
res.render('bar', {auth: req.isAuthenticated(), page_name: "myBars", bars: null});
} else {
res.render('bar', {auth: req.isAuthenticated(), page_name: "myBars", bars: bars});
}
});
} else {
res.render('bar', {auth: req.isAuthenticated(), page_name: "myBars", bars: null});
}
});
});
//contact us page
app.get('/contact', function (req, res) {
res.render('contact', {auth: req.isAuthenticated(), page_name: "contact", msg: ""});
});
// POST route from contact form
app.post('/contact', function (req, res) {
//setup smtp setting gmail
const smtpTrans = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: "contactpinpints@gmail.com", //username for gmail
pass: "zkkiniafavawgsyw" //secure app password
}
});
const mailOpts = {
from: req.body.email, //email address from form
to: 'contactpinpints@gmail.com', //send to pinpints address
subject: req.body.subject, //subject from form
//add name, email and messge from form
text: `${req.body.name} <${req.body.email}> says: ${req.body.msg}`
};
smtpTrans.sendMail(mailOpts, function (error, response) {
if (error) {
//set div to visable with failed message and allow user to close
res.render('contact', {auth: req.isAuthenticated(), msg: `<div class=\"alert alert-danger\" role=\"alert\">
<a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">×</a>Message failed to send</div>`});
}
else {
//set div to visable with sent message and allow user to close
res.render('contact', {auth: req.isAuthenticated(), msg: `<div class=\"alert alert-success\" role=\"alert\">
<a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">×</a>Message sent</div>`});
}
});
});
//handle 404 - Keep this as a last route
app.use(function (req, res) {
res.status(404);
res.render('404', {auth: req.isAuthenticated(), page_name: "404Error"});
});
app.listen(port);