forked from Hedgemon4/cosc304-only-ducks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
157 lines (137 loc) · 4.47 KB
/
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
const express = require('express')
const exphbs = require('express-handlebars')
const session = require('express-session')
const bodyParser = require('body-parser')
let index = require('./routes/index');
let loadData = require('./routes/loaddata')
let listOrder = require('./routes/listorder')
let listProd = require('./routes/listprod')
let addCart = require('./routes/addcart')
let showCart = require('./routes/showcart')
let checkout = require('./routes/checkout')
let order = require('./routes/order')
let login = require('./routes/login')
let validateLogin = require('./routes/validateLogin')
let logout = require('./routes/logout')
let admin = require('./routes/admin')
let product = require('./routes/product')
let displayImage = require('./routes/displayImage')
let customer = require('./routes/customer')
let ship = require('./routes/ship')
let signup = require('./routes/signup')
let createAccount = require('./routes/createAccount')
let editAccount = require('./routes/editAccount')
let viewOrders = require('./routes/viewOrders')
const app = express()
// Enable parsing of requests for POST requests
app.use(express.json())
app.use(bodyParser.urlencoded({extended: true}))
// This DB Config is accessible globally
dbConfig = {
user: 'SA',
password: 'YourStrong@Passw0rd',
server: 'db',
database: 'tempdb',
options: {
'enableArithAbort': true,
'encrypt': false,
}
}
// Setting up the session.
// This uses MemoryStorage which is not
// recommended for production use.
app.use(session({
secret: 'COSC 304 Rules!',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: false,
secure: false,
maxAge: 6000000,
}
}))
// Setting up the rendering engine
app.engine('handlebars', exphbs());
app.use(function (req, res, next) {
res.locals.session = req.session;
res.locals.body = req.body
next();
});
app.set('view engine', 'handlebars');
// Setting up Express.js routes.
// These present a "route" on the URL of the site.
// Eg: http://127.0.0.1/loaddata
app.use('/', index)
app.use('/loaddata', loadData)
app.use('/listorder', listOrder)
app.use('/listprod', listProd)
app.use('/addcart', addCart)
app.use('/showcart', showCart)
app.use('/checkout', checkout)
app.use('/order', order)
app.use('/createAccount', createAccount)
app.use('/login', login)
app.use('/validateLogin', validateLogin)
app.use('/logout', logout)
app.use('/admin', admin)
app.use('/product', product)
app.use('/displayImage', displayImage)
app.use('/customer', customer)
app.use('/ship', ship)
app.use('/signup', signup)
app.use('/editAccount', editAccount)
app.use('/viewOrders', viewOrders)
app.use(express.static(__dirname + '/public'))
// Handlebar helpers
let hbs = exphbs.create({});
hbs.handlebars.registerHelper('subtotal', function (price, quantity) {
return (quantity * price).toFixed(2)
})
hbs.handlebars.registerHelper('ordertotal', function (productList) {
let total = 0
for (let i = 0; i < productList.length; i++) {
let product = productList[i]
if (!product) {
continue
}
total = total + product.quantity * product.price
}
return total.toFixed(2)
})
hbs.handlebars.registerHelper('displaymoney', function (number) {
return Number(number).toFixed(2)
})
hbs.handlebars.registerHelper('getAddToCartLink', function (productId, productName, productPrice) {
return ("addcart?id=" + productId + "&name=" + escape(productName) + "&price=" + productPrice)
})
hbs.handlebars.registerHelper('login', function (session) {
if (session.authenticatedUser)
return '<a class="nav-link active nav-color" aria-current="page" href="/customer">' + session.authenticatedUser + '</a>'
else
return '<a class=\"nav-link active nav-color\" aria-current=\"page\" href="/login">Login</a>'
})
hbs.handlebars.registerHelper('displayDate', function (date){
return new Date(date).toLocaleDateString("en-CA")
})
hbs.handlebars.registerHelper('getProductDescriptionLink', function(productId){
return ("product?id=" + productId)
})
hbs.handlebars.registerHelper('displayAdmin', function (session){
return session.isAdmin
})
hbs.handlebars.registerHelper('selected', function(option, value){
if (option === value) {
return 'selected';
} else {
return ''
}
})
hbs.handlebars.registerHelper('selected', function(option, value) {
if (option === value) {
return 'selected';
} else {
return ''
}
})
// Starting our Express app
app.listen(3000)