-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
267 lines (212 loc) · 8.72 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
/*===========================================================================
HTML5 Express Boilerplate ("H5EB")
============================================================================= */
/*===========================================================================
TODO: JSHINT GLOBAL VARS, CONFIG, AND INSTRUCTIONS
============================================================================= */
/*===========================================================================
DEPENDENCIES
============================================================================= */
var express = require('express'),
inspect = require('inspect'),
colors = require('colors'),
sys = require('util'),
mime = require('mime'),
cleanCSS = require('clean-css'),
app = module.exports = express.createServer();
/*===========================================================================
SETTINGS
============================================================================= */
var port = 4000,
cacheAge = 60000 * 60 * 24 * 365,
logs = {
set: false,
string: '\\n ' + ':date'.bold.underline + '\\n\\n' + ' IP: '.cyan.bold
+ ' ' + ':remote-addr'.white + '\\n' + ' Method: '.red.bold
+ ':method'.white + '\\n' + ' URL: '.blue.bold + ':url'.white
+ '\\n' + ' Status: '.yellow.bold + ':status'.white + '\\n'
+ ' User Agent: '.magenta.bold + ':user-agent'.white
};
/*===========================================================================
DEFAULT CONFIG
============================================================================= */
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.favicon(__dirname + '/public/favicon.ico'));
app.use(app.router);
if(logs.set) app.use(express.logger(logs.string));
});
/*===========================================================================
DEVELOPMENT CONFIG
$ node server.js
============================================================================= */
app.configure('development', function() {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({
dumpExceptions : true,
showStack : true
}));
});
/*===========================================================================
PRODUCTION CONFIG
$ NODE_ENV=production node server.js
============================================================================= */
app.configure('production', function() {
port = 8080;
app.use(express.static(__dirname + '/public', { maxAge: cacheAge }));
app.use(express.errorHandler());
});
/*===========================================================================
ERROR HANDLING
============================================================================= */
// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.
app.use(function(req, res, next){
// the status option, or res.statusCode = 404
// are equivalent, however with the option we
// get the "status" local available as well
res.render('404', {
layout: false,
status: 404,
//url: req.url,
title: '404 Error'
});
});
// error-handling middleware, take the same form
// as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).
// when connect has an error, it will invoke ONLY error-handling
// middleware.
// If we were to next() here any remaining non-error-handling
// middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware
// would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
res.render('500', {
layout: false,
status: err.status || 500,
error: err,
title: '500 Error'
});
});
/*===========================================================================
YOUR ROUTES
============================================================================= */
// Index
app.get('/', function(req, res) {
res.render('index', {
modernizr: "javascripts/libs/modernizr-2.0.6.min.js",
jquery: "javascripts/libs/jquery-1.7.2.min.js",
title: 'this is a title',
description: 'this is a description',
javascripts: ["javascripts/script.js"],
stylesheets: ["style.css"]
});
});
/*===========================================================================
ERROR TESTING
============================================================================= */
/*
// 404 Error
app.get('/404', function(req, res, next){
next();
});
// 403 Error
app.get('/403', function(req, res, next){
var err = new Error('not allowed!');
err.status = 403;
next(err);
});
// 500 Error
app.get('/500', function(req, res, next){
next(new Error('keyboard cat!'));
});
*/
/*===========================================================================
WILDCARD MIDDLEWARE
============================================================================= */
function wildcard(req, res, next) {
/*=========================================================================
BETTER WEBSITE EXPERIENCE FOR IE USERS
=========================================================================== */
var url = req.url,
ua = req.headers['user-agent'],
reqPath = req.url;
if(ua && ua.indexOf('MSIE'))
res.setHeader('X-UA-Compatible', 'IE=Edge,chrome=1');
// We don't want to send this header on _everything_
if(reqPath.match(/\.(js|css|gif|png|jpe?g|pdf|xml|oga|ogg|m4a|ogv|mp4|m4v|webm|svg|svgz|eot|ttf|otf|woff|ico|webp|appcache|manifest|htc|crx|xpi|safariextz|vcf)$/)) {
res.removeHeader('X-UA-Compatible');
res.removeHeader('IE=Edge,chrome=1');
}
/*=========================================================================
CORS
=========================================================================== */
// Force the latest IE version, in cases when it may fall back to IE7 mode
// http://github.com/rails/rails/commit/123eb25#commitcomment-118920
// Use ChromeFrame if it's installed, for a better experience with IE folks
// Control cross domain using CORS http://enable-cors.org
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
/*=========================================================================
MIME TYPES
=========================================================================== */
// Until pull request is in
// https://github.com/bentomas/node-mime
mime.define({
'font/opentype' : ['otf'],
'text/cache-manifest' : ['appcache', 'manifest'],
'text/x-component' : ['htc'],
'application/x-chrome-extension' : ['crx'],
});
// TODO: Allow concatenation from within specific js and css files
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L111
// TODO: Gzip compression
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L137
// TODO: Expires headers (for better cache control)
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L186
// TODO: Stop screen flicker in IE on CSS rollovers
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L271
// TODO: Cookie setting from iframes
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L286
// TODO: Suppress or force the "www." at the beginning of URLs
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L315
// TODO: Built-in filename-based cache busting
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L356
// TODO: Prevent SSL cert warnings
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L376
// TODO: UTF-8 encoding
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L411
// TODO: Block access to "hidden" directories whose names begin with a period
// https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess/#L442
next();
}
/*===========================================================================
ALWAYS KEEP THIS ROUTE LAST
============================================================================= */
app.use(wildcard);
/*===========================================================================
START EXPRESS SERVER
============================================================================= */
app.listen(port);
var appPort = app.address().port + '',
appEnv = app.settings.env.toUpperCase();
sys.puts(''
+ 'SERVER LISTENING ON PORT '.rainbow
+ " ".white.inverse
+ appPort.white.inverse
+ " ".white.inverse
+ ' IN '.rainbow
+ " ".white.inverse
+ appEnv.white.inverse
+ " ".white.inverse
+ ' MODE '.rainbow
);