forked from ccoenraets/backbone-directory
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathserver.js
108 lines (93 loc) · 3.07 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
//setup Dependencies
var config = require('./config.js');
var express = require('express');
var RedisStore = require('connect-redis')(express);
var sessionStore = new RedisStore(config.redis);
var mongoose = require('mongoose');
var useragent = require('./lib/useragent.js');
var employee = require('./lib/employee.js');
var staticdir = '/static'; // common content
var webdir = '/web';
var iphonedir = '/iphone';
var mobiledir = '/jquerymobile';
// Connect to data
mongoose.connect(config.mongodb);
// Init seed data - this may not be needed in your application
employee.seed();
// Setup server
var app = express.createServer();
app.listen(config.port);
var io = require('./lib/chat.js')(app);
var assetMiddleware = require('./lib/asset.js');
app.configure(function() {
app.set('views', __dirname+'/views');
app.set('view options', { layout:false });
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.staticCache());
app.use(assetMiddleware);
app.use(express.session({ 'store':sessionStore, secret:config.sessionSecret }));
app.use(staticdir, express.static(__dirname+staticdir));
app.use(webdir, express.static(__dirname+webdir));
app.use(iphonedir, express.static(__dirname+iphonedir));
app.use(mobiledir, express.static(__dirname+mobiledir));
app.use(app.router);
});
// Make assets available to index.ejs
app.dynamicHelpers({
'assetsCache': function(req, res) {
return assetMiddleware.cacheHashes;
},
'isProduction': function(req, res) {
return 'production' === config.environment;
}
});
//setup the errors
app.error(function(err, req, res, next) {
if (err instanceof NotFound) {
res.render('404.jade', { locals:{
title:'404 - Not Found',
description:'',
author:'',
analyticssiteid:'XXXXXXX'
}, status:404 });
} else {
res.render('500.jade', { locals:{
title:'The Server Encountered an Error',
description:'',
author:'',
analyticssiteid:'XXXXXXX',
error:err
}, status:500 });
}
});
///////////////////////////////////////////
// Routes //
///////////////////////////////////////////
/////// ADD ALL YOUR ROUTES HERE /////////
// Index route - depends upon the useragent
app.get('/', function(req, res) {
useragent(req, res);
var index = req.useragent.ios ? iphonedir : (req.useragent.mobile ? mobiledir : webdir);
console.log('index: ', index, config.environment);
res.render(__dirname+index+'/index.ejs')
});
// API routes return JSON
app.get('/api/employees', employee.getEmployees);
app.get('/api/employees/:id', employee.getEmployee);
app.get('/api/employees/:id/reports', employee.getReports);
app.get('/api/employees/search/:query', employee.findByName);
//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();
});
function NotFound(msg) {
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
console.log('Listening on http://0.0.0.0:' + config.port);