-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
351 lines (258 loc) · 9.71 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var express = require('express')
, routes = require('./routes')
, http = require('http')
, useragent = require('express-useragent');
// template engine
var hbs = require('hbs');
// mongodb
var mongo = require('mongodb');
//var Server = require('mongodb').Server;
var mongodbhost = 'yourid.mongolab.com'
, mongodbport = 'yourport'
, mongodbuser = 'user'
, mongodbpassword = 'password'
, mongodbname = 'dbname';
//var rs = require('./record_model.js');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
/*
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
*/
// Apply basic authentication to server
app.use(express.basicAuth('admin', 'password'));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.engine('html', require('hbs').__express);
app.set('views', __dirname + '/views/html');
app.set('view engine', 'html');
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// invoke useragent sniffing module
app.use(useragent.express());
/* google webmaster and sitemap.xml */
function generate_xml_sitemap() {
// this is the source of the URLs on your site, in this case we use a simple array, actually it could come from the database
var urls = ['/'];
// the root of your website - the protocol and the domain name with a trailing slash
var root_path = 'http://www.yourdomain.com/';
// XML sitemap generation starts here
var priority = 0.5;
var freq = 'monthly';
var xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
for (var i in urls) {
xml += '<url>';
xml += '<loc>'+ root_path + urls[i] + '</loc>';
xml += '<changefreq>'+ freq +'</changefreq>';
xml += '<priority>'+ priority +'</priority>';
xml += '</url>';
i++;
}
xml += '</urlset>';
return xml;
}
app.get('/sitemap.xml', function(req, res) {
var sitemap = generate_xml_sitemap(); // get the dynamically generated XML sitemap
res.header('Content-Type', 'text/xml');
res.send(sitemap);
})
// UTILITY ROUTE HANDLING
// loader.io
app.get('/loaderio-12345.html', function(req, res){
res.send('loaderio-12345');
});
// for testing mobile
app.get('/mobile', function(req, res){
//res.render("m.html");
res.send("There is no mobile site ...yet.");
});
/*
Mongolab example test query:
https://api.mongolab.com/api/1/databases/databasename/collections/content?q={'locale':{$exists:true}}&s=&f=&apiKey=12345
*/
// ROUTE HANDLING
// User of an age gate (should you choose to use one)
/*
app.get('/', function(req, res){
// To Get a Cookie
var cookies = {};
req.headers.cookie && req.headers.cookie.split(';').forEach(function( cookie ) {
var parts = cookie.split('=');
cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim();
//console.log(cookies.userAge);
if (cookies.userAge != undefined && cookies.userAge > 17) {
//console.log('jeeeyuh');
res.redirect('/site');
}
});
res.render('index.html', { layout : false , 'title' : 'demo'});
});
*/
// LIST VIEW
app.get('/cms', function(req, res){
var listData = function(err, collection) {
collection.find({ 'locale': {$exists: true}}).toArray(function(err, results) {
res.render('index.html', { layout : false , 'title' : 'demo', 'results' : results });
});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', listData);
//client.close();
});
});
})
// SPECIFY VIEW LOCALE
// Uncomment this if you want to go localized
/*
app.get('/:locale', function(req, res){ // en_US
var ObjectID = require('mongodb').ObjectID;
var listData = function(err, collection) {
var chosenLocale = req.params.locale;
collection.findOne({'locale' : chosenLocale} , function(err, results) {
res.render('home.html', { layout : false , 'title' : 'demo', 'results' : results });
});
}
*/
app.get('/', function(req, res){
var ObjectID = require('mongodb').ObjectID;
var listData = function(err, collection) {
collection.findOne({'locale' : 'en_US'} , function(err, results) {
res.render('home.html', { layout : false , 'title' : 'demo', 'results' : results });
});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', listData);
//client.close();
});
});
});
// CREATE
app.get('/add_record', function(req, res){
res.render('add.html', { layout : false , 'title' : 'demo'});
})
app.post('/save_record', function(req, res){
console.log(req.body);
var data = {'content1' : req.body.content1 , 'content2' : req.body.content2, 'content3' : req.body.content3, 'content4' : req.body.content4, 'content5' : req.body.content5, 'content6' : req.body.content6,
'locale' : req.body.locale, 'status' : req.body.status };
var insertData = function(err, collection) {
collection.insert(data, function() {});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', insertData);
//client.close();
});
});
res.redirect('/');
});
// EDIT VIEW
app.get('/edit_record/:id', function(req, res){
var ObjectID = require('mongodb').ObjectID;
var listData = function(err, collection) {
var chosenId = new ObjectID(req.params.id);
collection.findOne({'_id' : chosenId} , function(err, results) {
res.render('edit.html', { layout : false , 'title' : 'demo', 'results' : results });
});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', listData);
//client.close();
});
});
});
// EDIT UPDATE
app.post('/update_record', function(req, res){
console.log(req.body);
var ObjectID = require('mongodb').ObjectID;
var data = {'content1' : req.body.content1 , 'content2' : req.body.content2, 'content3' : req.body.content3, 'content4' : req.body.content4, 'content5' : req.body.content5, 'content6' : req.body.content6,
'locale' : req.body.locale, 'status' : req.body.status };
var updateData = function(err, collection) {
var chosenId = new ObjectID(req.body.id);
collection.update({"_id": chosenId}, {$set: data }, function() {});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', updateData);
//client.close();
});
});
res.redirect('/');
});
// DELETE
app.get('/delete_record/:id', function(req, res){
var ObjectID = require('mongodb').ObjectID;
var removeData = function(err, collection) {
var chosenId = new ObjectID(req.params.id);
collection.remove({'_id' : chosenId}, function() {});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', removeData);
//client.close();
});
});
res.redirect('/');
});
// MEDIA - EDIT VIEW
app.get('/edit_media', function(req, res){
var ObjectID = require('mongodb').ObjectID;
var listData = function(err, collection) {
collection.findOne({'type' : 'media'} , function(err, results) {
//console.log(results);
res.render('editmedia.html', { layout : false , 'title' : 'demo', 'results' : results });
});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', listData);
//client.close();
});
});
});
// MEDIA - EDIT UPDATE
app.post('/update_media', function(req, res){
console.log(req.body);
var ObjectID = require('mongodb').ObjectID;
var data = {'media1' : req.body.media1
, 'media2' : req.body.media2
, 'media3' : req.body.media3
, 'media4' : req.body.media4
, 'media5' : req.body.media5
, 'media6' : req.body.media6
, 'media7' : req.body.media7
};
var updateData = function(err, collection) {
var chosenId = new ObjectID(req.body.id);
collection.update({"_id": chosenId}, {$set: data }, function() {});
}
var db = new mongo.Db(mongodbname, new mongo.Server(mongodbhost, mongodbport, {}), {w: 1});
db.open(function(err, client) {
client.authenticate(mongodbuser, mongodbpassword, function(err, p_client) {
client.collection('content', updateData);
//client.close();
});
});
res.redirect('/');
});
// END ROUTE
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});