-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
199 lines (165 loc) · 4.76 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
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
var express = require('express');
var hbs = require('express-handlebars');
var bodyParser = require('body-parser');
var analytics = require('./config').analytics;
var app = express();
var user = {};
user.cart = {};
user.cart.products = [];
user.cart.total = 0;
var products = [];
var shipping = [];
var payment = [];
/** WARNING
This code is a simplification with no standards, you shlould not use this like an example of how to program in node js. There are many good practices that are not being used on purpose.
**/
/* BASIC CONFIGURATION */
//route static files
app.use(express.static('public'));
//set handlebars layout engine
app.engine('.hbs', hbs({defaultLayout: 'default', extname: '.hbs'}));
app.set('view engine', '.hbs');
//guest init middleware, we will use a memory variable for the guest (shared with all sessions (is just an example))
app.use(function(req, res, next) {
req.user = user;
res.locals.user = user;
next();
});
//body parser for post forms
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(function(req, res, next) {
res.locals.analytics = analytics;
next();
})
//generate 40 products and assigned them to a category
//product generation
for(var i = 0; i < 40; i++) {
var product = {};
product.id = i + 1;
product.name = 'Product ' + (i + 1);
product.price = 100 % ((i + 5)*3);
product.category = 2 - (i % 2);
product.brand = 'Example';
product.category2 = 'NodeJS';
products.push(product);
}
//set shipping methods
shipping.push({id: 1, name: 'Pickup in Store', price: 0});
shipping.push({id: 2, name: 'Deliver Home', price: 5});
//set shipping methods
payment.push({id: 1, name: 'Visa'});
payment.push({id: 2, name: 'American Express'});
/* ROUTES */
//add to cart
app.post('/product/addToCart', function(req, res) {
var id = req.body.id;
var product = products.filter(function(p) {
return p.id== id;
})[0];
user.cart.products.push(product);
var total = 0;
for(var i = 0, len = user.cart.products.length; i < len; i++) {
total += user.cart.products[i].price;
}
user.cart.total = total;
res.json({added: true});
})
//home
app.get('/', function (req, res) {
var data = {};
data.analytics = res.locals.analytics;
data.products = products.slice(0,4);
res.render('index', data);
});
//categories
app.get('/category/:id', function(req, res) {
var id = req.params.id;
var c_products = products.filter(function(p) {
return p.category == id;
});
var data = {};
data.analytics = res.locals.analytics;
data.category = {id: id};
data.products = c_products;
res.render('category', data);
});
//product
app.get('/product/:id', function(req, res) {
var id = req.params.id;
var p_products = products.filter(function(p) {
return p.id== id;
})[0];
var data = {};
data.analytics = res.locals.analytics;
data.product = p_products;
res.render('product', data);
});
//shopping cart
app.get('/cart', function(req, res) {
var data = {};
data.analytics = res.locals.analytics;
data.total = req.user.cart.total;
data.products = req.user.cart.products;
res.render('cart', data);
})
//shipping
app.get('/checkout/shipping', function(req, res) {
var data = {};
data.analytics = res.locals.analytics;
data.total = req.user.cart.total;
data.products = req.user.cart.products;
res.render('shipping', data);
})
//payment
app.post('/checkout/payment', function(req, res) {
var id = req.body.shipping;
var c_shipping = shipping.filter(function(p) {
return p.id == id;
})[0];
//calucate cart total
user.cart.shipping = c_shipping;
var total = 0;
for(var i = 0, len = user.cart.products.length; i < len; i++) {
total += user.cart.products[i].price;
}
total += user.cart.shipping.price;
user.cart.total = total;
var data = {};
data.analytics = res.locals.analytics;
data.total = user.cart.total;
data.products = req.user.cart.products;
data.shipping = user.cart.shipping;
res.render('payment', data);
})
//confirmation
app.post('/checkout/summary', function(req, res) {
var id = req.body.payment;
var c_payment = payment.filter(function(p) {
return p.id == id;
})[0];
//calucate cart total
user.cart.payment = c_payment;
var total = 0;
for(var i = 0, len = user.cart.products.length; i < len; i++) {
total += user.cart.products[i].price;
}
total += user.cart.shipping.price;
user.cart.total = total;
var purchase = user.cart;
user.cart = {};
user.cart.products = [];
user.cart.total = 0;
var data = {};
data.analytics = res.locals.analytics;
data.total = purchase.total;
data.products = purchase.products;
data.shipping = purchase.shipping;
data.payment = purchase.payment;
res.render('summary', data);
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});