-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
268 lines (233 loc) · 5.78 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Module dependencies.
*/
var sys = require('sys'),
express = require('express'),
http = require('http'),
Collection = require('./collection'),
Config = require('./config'),
Product = require('./product');
var app = module.exports = express.createServer();
function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
//setup the errors
app.error(function(err, req, res, next) {
if (err instanceof NotFound) {
res.render('404', { locals: {
header: '#Header#'
,footer: '#Footer#'
,title : '404 - Not Found'
,description: ''
,author: ''
,analyticssiteid: 'XXXXXXX'
},status: 404, layout: false});
} else {
res.render('500', { locals: {
header: '#Header#'
,footer: '#Footer#'
,title : 'The Server Encountered an Error'
,description: ''
,author: ''
,analyticssiteid: 'XXXXXXX'
,error: err
}, status: 500 , layout: false});
}
});
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
console.log("using config for 'development'");
});
app.configure('production', function() {
console.log("using config for 'production'");
});
// Routes
app.get('/', function(req, res) {
res.render('index', {
locals: {
title: 'Home'
}
});
});
app.get('/products/page/:page([0-9]+)', function(req, res) {
console.log("loading products");
var page = parseInt(req.params.page);
var pages = parseInt(req.count / Config.pageLimit) + ((req.count % Config.pageLimit)?1:0);
if (page > pages) {
throw new NotFound('Page not found.');
} else {
res.render('products', {
locals: {
title: 'Products'
},
'products': req.products,
'current': page,
'pages': pages,
'productsCount': req.count
});
}
});
app.get('/products/:productId', function(req, res) {
console.log("loading product");
res.render('product', {
locals: {
title: req.product.title
},
'product': req.product
});
});
app.get('/products', function(req, res) {
console.log("loading products");
res.redirect('/products/page/1');
});
app.get('/collections', loadCollections, function(req, res) {
console.log("loading collections");
res.render('collections', {
locals: {
title: 'Collections'
},
'collections': req.collections
});
});
app.get('/collections/:collectionId', function(req, res) {
console.log("loading collection");
res.render('collection', {
locals: {
title: req.collection.title
},
'products': req.products,
'collection': req.collection
});
});
app.post('/products', createProduct, function(req, res) {
console.log("create product");
res.redirect('/products/' + req.product.id);
});
// A Route for Creating a 500 Error (Useful to keep around)
app.get('/500', function(req, res){
throw new Error('This is a 500 Error');
});
//The 404 Route (ALWAYS Keep this as the last route)
app.get('/*', function(req, res){
throw new NotFound;
});
// Route Param Preconditions
app.param('productId', function(req, res, next, id) {
Product.load(id, function(err, product) {
if (err) {
return next(err);
}
if (!product) {
return next(new Error('failed to find product'));
}
req.product = product;
next();
});
})
app.param('collectionId', function(req, res, next, id) {
Collection.load(id, function(err, collection) {
if (err) {
return next(err);
}
if (!collection) {
return next(new Error('failed to find collection'));
}
req.collection = collection;
next();
});
})
// Helper methods
app.param('collectionId', function(req, res, next, id) {
Product.listByCollection(id, function(err, products) {
if (err) {
return next(err);
}
if (!products) {
return next(new Error('failed to find collection products'));
}
req.products = products;
next();
});
})
app.param('page', function(req, res, next, page) {
Product.count(function(err, count) {
if (err) {
return next(err);
}
if (!count) {
return next(new Error('failed to count products'));
}
req.count = count;
next();
});
})
app.param('page', function(req, res, next, page) {
var limit = Config.pageLimit;
Product.list(page, limit, function(err, products) {
if (err) {
return next(err);
}
if (!products) {
return next(new Error('failed to find products'));
}
req.products = products;
next();
});
})
function loadProductsByCollection(req, res, next) {
Product.list(req.collectionId, function(err, products) {
if (err) {
return next(err);
}
if (!products) {
return next(new Error('failed to find products'));
}
req.products = products;
next();
});
}
function loadCollections(req, res, next) {
Collection.list(function(err, collections) {
if (err) {
return next(err);
}
if (!collections) {
return next(new Error('failed to find collections'));
}
req.collections = collections;
next();
});
}
function createProduct(req, res, next) {
var newProduct = {};
newProduct.title = "TEST Product";
newProduct.vendor = "vendor 1";
newProduct.product_type = "type 1";
newProduct.options = [{"name" : "size"}];
Product.create(newProduct, function(err, product) {
if (err) {
return next(err);
}
if (!product) {
return next(new Error('failed to create product'));
}
req.prodúct = product;
next();
});
}
// Only listen on $ node app.js
if (!module.parent) {
app.listen(11757);
console.log("Express server listening on port %d", app.address().port)
}