forked from Mirocow/javascript-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·135 lines (91 loc) · 3.2 KB
/
app.js
File metadata and controls
executable file
·135 lines (91 loc) · 3.2 KB
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
//require("time-require");
const fs = require('fs');
const config = require('config');
require('cls'); // init CLS namespace once, handler used below
const Application = require('application');
const app = new Application();
if (process.env.NODE_ENV != 'development') {
// only log.error in prod, otherwise just die
process.on('uncaughtException', function(err) {
// let bunyan handle the error
app.log.error({
message: err.message,
name: err.name,
errors: err.errors,
stack: err.stack
});
process.exit(255);
});
}
// The app is always behind Nginx which serves static
// (Maybe behind Cloudflare as well)
// trust all headers from the proxy
// X-Forwarded-Host
// X-Forwarded-Proto
// X-Forwarded-For -> ip
app.proxy = true;
// ========= Helper handlers ===========
app.requireHandler('cls');
app.use(function*(next) {
this.countryCode = (this.get('cf-ipcountry') || this.get('x-nginx-geo') || '').toLowerCase();
if (this.countryCode == 'xx') this.countryCode = ''; // CloudFlare cannot detect country
yield* next;
});
app.requireHandler('mongooseHandler');
app.requireHandler('requestId');
app.requireHandler('requestLog');
app.requireHandler('nocache');
//app.requireHandler('time');
// this middleware adds this.render method
// it is *before errorHandler*, because errors need this.render
app.requireHandler('render');
// errors wrap everything
app.requireHandler('errorHandler');
// this logger only logs HTTP status and URL
// before everything to make sure it log all
app.requireHandler('accessLogger');
// before anything that may deal with body
// it parses JSON & URLENCODED FORMS,
// it does not parse form/multipart
app.requireHandler('bodyParser');
// parse FORM/MULTIPART
// (many tweaks possible, lets the middleware decide how to parse it)
app.requireHandler('multipartParser');
// right after parsing body, make sure we logged for development
app.requireHandler('verboseLogger');
if (process.env.NODE_ENV == 'development') {
// app.verboseLogger.addPath('/:any*');
}
app.requireHandler('conditional');
app.requireHandler('session');
app.requireHandler('passportSession');
app.requireHandler('passportRememberMe');
app.requireHandler('lastActivity');
app.requireHandler('csrf');
app.requireHandler('flash');
app.requireHandler('paymentsMethods');
// ======== Endpoint services that actually generate something ==========
var endpoints = [];
if (process.env.NODE_ENV == 'development') {
endpoints.push('markup', 'dev');
}
endpoints.push(
'users', 'auth', 'ebook', 'cache', 'search', 'profile', 'jb', 'play', 'nodejsScreencast', 'about', 'imgur',
'profileGuest', 'quiz', 'currencyRate', 'payments', 'download', 'staticPage', 'newsletter', 'mailer', 'courses'
);
endpoints.forEach(function(name) {
app.requireHandler(name);
});
if (fs.existsSync(config.extraHandlersRoot)) {
fs.readdirSync(config.extraHandlersRoot).forEach(function(extraHandler) {
if (extraHandler[0] == '.') return;
app.requireHandler(extraHandler);
});
}
// stick to bottom to detect any not-yet-processed /:slug
app.requireHandler('tutorial');
// must be last
app.requireHandler('404');
// uncomment for time-require to work
//process.emit('exit');
module.exports = app;