-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.js
193 lines (156 loc) · 5.61 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
/*!
* Opower Jobs
* Copyright(c) 2010 Dylan Greene <[email protected]>
* MIT Licensed
*/
var log = require('logging').from(__filename),
Express = require('express'),
Assets = require('./lib/assets'),
ReferralHandler = require('./lib/referralHandler'),
ContentHandler = require('./lib/contentHandler'),
JobHandler = require('./lib/jobHandler'),
Server = module.exports = Express.createServer();
var VIEWS = __dirname + '/views',
PUBLIC = __dirname + '/public',
PORT = parseInt(process.env.PORT || 3000),
HOSTNAME = 'opowerjobs.com';
var TEMP_HOSTS = { '8.17.80.250': 1, '72.2.126.71': 1, 'opower.no.de': 1, 'dylan95.com': 1, 'www.dylan95.com': 1, 'dylangreene.com': 1, 'www.dylangreene.com': 1, 'coursereviews.com': 1, 'www.coursereviews.com': 1, 'teacherreviews.com': 1, 'www.teacherreviews.com': 1 };
// hack for testing production settings.
if (PORT != 3000 || process.env.JOYENT) {
Server.set('env', 'production');
}
//in case of crash. I've never seen this used, got it from somebody else's code.
process.title = 'opowerjobs';
/*
process.addListener('uncaughtException', function (err, stack) {
log('*************************************');
log('************EXCEPTION****************');
log('*************************************');
err.message && log(err.message);
err.stack && log(err.stack);
log('*************************************');
});
*/
function production(){
log('running in production mode');
Server.locals({
href: function(url) { return 'http://' + HOSTNAME + (url[0] == '/' ? '' : '/') + url; },
production: true
});
}
function development() {
Server.locals({
href: function(url) { return (url[0] == '/' ? '' : '/') + url; },
production: false
});
log('running in development mode');
Server.use(Express.errorHandler({ dumpExceptions: true, showStack: true }));
}
function common() {
Server.set('views', VIEWS);
Server.locals({
log: log,
array: function(obj) {
return obj.map(function(value, id) {
return { id: id, value: value };
});
}
});
Server.dynamicHelpers({
session: function(req){
return req.session;
},
current_url: function(res, req, next) {
return res.url;
},
current_host: function(res, req, next) {
return res.headers.host;
}
});
Server.use(Express.cookieParser());
Server.use(Express.session({ secret: 'OPOWER!' }));
Server.use(Express.bodyParser());
Server.use(Express.favicon(PUBLIC + '/favicon.ico'));
Server.use(Express.static(PUBLIC, { maxAge: 31557600000 })); //oneYear
Server.use(Server.router);
Server.locals({
currentPageID: false,
pages: []});
}
Server.configure('development', development);
Server.configure('production', production);
Server.configure(common);
Server.error(function(err, req, res, next){
if (err.message != 'EISDIR, Is a directory') {
log('****************ERROR****************');
log('http://' + req.headers.host + req.url);
err.message && log(err.message);
err.arguments && log(err.arguments);
err.stack && log(err.stack);
log('*************************************');
//next();
}
if (Server.get('env') == 'production') {
res.redirect('/');
}
});
// For Google Webmaster
Server.get('/google97924ebf42be7c40.html', function(req, res){
res.send('google-site-verification: google97924ebf42be7c40.html');
res.end();
});
Server.get('/nagios', function(req, res) {
res.send('I am working fine.');
res.end();
});
// Get rid of urls that end in / - makes Google Analytics easier to read
Server.get(/^.+\/$/, function(req, res){
res.redirect(req.url.substr(0, req.url.length - 1));
});
// Redirect other servers to the main one
Server.get(/^/, function(req, res, next){
var host = req.headers.host.split(':')[0];
if (TEMP_HOSTS[host]) {
req.session.tempHost = host;
}
if (host != 'localhost' && host != HOSTNAME && !TEMP_HOSTS[host]) {
var new_url = 'http://' + HOSTNAME + req.originalUrl;
//log('redirect from:', req.headers.host + req.originalUrl, 'to', new_url);
res.redirect(new_url);
} else {
next();
}
});
Assets.addHandler({Server: Server, hostname: HOSTNAME });
ReferralHandler.addHandlers( {Server: Server } );
ContentHandler.addHandlers( {Server: Server } );
JobHandler.addHandlers( {Server: Server } );
Server.get('/log', function(req, res) {
res.render('log.ejs', { locals: { history: log.history() } });
});
// Required for 404's to return something
Server.get('/*', function(req, res){
var host = (req.headers.host || '').split(':')[0],
new_url,
extension = req.url.match(/\....$/);
if (extension) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Cannot ' + req.method + ' ' + req.url);
}
else if (TEMP_HOSTS[host]) {
res.redirect('http://' + HOSTNAME);
}
else {
if (!req.session.tempHost || host == 'localhost') {
if (req.header('user-agent', '').match(/msnbot|slurp/i) === null) {
log('404', req.url, req.header('referer') || req.session.jobboard || req.header('user-agent', ''));
}
}
var array = req.url.replace(/\/\//g, '/').split('/');
if (array.pop() == '') { array.pop(); }
new_url = array.join('/') || '/';
res.redirect(new_url);
}
});
Server.listen(PORT, null);
log('Starting OPOWER JOBS on', PORT);