-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (88 loc) · 3.36 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
var express = require('express'),
util = require('util'),
device = require('express-device'),
cons = require('consolidate'),
hogan = require('hogan.js'),
routes = require('./routes'),
languages = require('./languages'),
helpers = require('./helpers.js');
var app = express();
function setLanguage(req, res, next) {
if (typeof req.params.lang !== 'undefined' && languages.isSupported(req.params.lang)) {
console.log('setting language \'' + req.params.lang + '\'');
res.clearCookie('lang');
res.cookie('lang', req.params.lang, { path: '/' });
req.cookies.lang = req.params.lang;
hogan.cache = {};
} else if (typeof req.cookies.lang === 'undefined') {
console.log('setting to default language: \'' + languages.default.code +'\'');
res.cookie('lang', languages.default.code, { path: '/' });
}
if(next) next();
}
function PageNotFoundError(message){
this.name = 'PageNotFoundError';
Error.call(this, message);
Error.captureStackTrace(this, arguments.callee);
}
PageNotFoundError.prototype.__proto__ = Error.prototype;
// Configuration
app.configure(function(){
app.engine('html', cons.hogan);
// assign .html as the default extension
app.set('view engine', 'html');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(device.capture());
app.enableViewRouting();
app.setCommonHelpers();
app.enableDeviceHelpers();
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next) {
console.log('reached PageNotFoundError use');
next(new PageNotFoundError())
});
app.use(function(err, req, res, next) {
if (err instanceof PageNotFoundError) {
console.log('got a 404');
res.redirect('/');
} else if (req.xhr) {
var lang = req.cookies.lang;
if (!lang || !languages.isSupported(lang)) lang = languages.default.code;
console.log('resource ' + err.name + ' for language ' + lang);
var errorTranslation = languages[lang].translate(err.name, err.arguments);
if(errorTranslation && errorTranslation !== err.name) {
console.log('error in ajax req:' + errorTranslation);
res.send(errorTranslation, 500);
} else {
console.log('error in ajax req:' + err.message);
res.send(err.message, 500);
}
} else {
console.log('error:' + err.message);
res.render('500', {
status: 500,
error: util.inspect(err),
showDetails: app.settings.showErrorDetails
});
}
});
});
// Routes
app.get('/*', setLanguage);
app.get('/', routes.index);
app.get('/throwError', routes.throwError);
app.post('/toDecimal', routes.toDecimal);
app.post('/fromDecimal', routes.fromDecimal);
app.get('/language/:lang', function (req, res) {
console.log('going to change language');
setLanguage(req, res);
res.redirect('home');
});
var port = process.env.PORT || 3000;
app.listen(port);
console.log("Express server listening on port %d in %s mode", port, app.settings.env);