forked from joshbeckman/thecolorapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (66 loc) · 1.92 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
require('newrelic');
var express = require('express')
, http = require('http')
, path = require('path')
, fs = require('fs')
, cors = require('cors')
, config = JSON.parse(fs.readFileSync('./static/config.json'))
, app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(cors());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {
res.render('404', { url: req.url, title: '404 - '+config.name });
return;
}
if (req.accepts('json')) {
res.send(404, config.status['404']);
return;
}
res.type('txt').send('404: Not found');
});
function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.send(500, config.status['500']);
} else {
next(err);
}
}
function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
// development only
app.configure('development', function(){
app.use(express.errorHandler({ showStack: true }));
var repl = require('repl').start('liverepl> ');
// repl.context.io = io;
// repl.context.Post = Post;
});
// Set up routes
require('./routes/frontEnd')(app, ensureAuth);
require('./routes/api')(app, ensureAuth);
function ensureAuth(req, res, next) {
if(req.query.key && req.query.key == process.env.API_KEY) {return next();}
res.send(403, config.status['403']);
}
http.createServer(app).listen(app.get('port'), function(){
console.log(config.name + " server listening on port %d in %s mode", app.get('port'), app.settings.env);
});