1 |
|
2 |
|
3 |
|
4 |
|
5 | var mixin = require('utils-merge');
|
6 | var escapeHtml = require('escape-html');
|
7 | var Router = require('./router');
|
8 | var methods = require('methods');
|
9 | var middleware = require('./middleware/init');
|
10 | var query = require('./middleware/query');
|
11 | var debug = require('debug')('express:application');
|
12 | var View = require('./view');
|
13 | var http = require('http');
|
14 | var compileTrust = require('./utils').compileTrust;
|
15 | var deprecate = require('./utils').deprecate;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var app = exports = module.exports = {};
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | app.init = function(){
|
34 | this._baseRoutes = {};
|
35 | this.cache = {};
|
36 | this.settings = {};
|
37 | this.engines = {};
|
38 | this.defaultConfiguration();
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | app.defaultConfiguration = function(){
|
48 |
|
49 | this.enable('x-powered-by');
|
50 | this.enable('etag');
|
51 | var env = process.env.NODE_ENV || 'development';
|
52 | this.set('env', env);
|
53 | this.set('subdomain offset', 2);
|
54 | this.set('trust proxy', false);
|
55 |
|
56 | debug('booting in %s mode', env);
|
57 |
|
58 |
|
59 | this.on('mount', function(parent){
|
60 | this.request.__proto__ = parent.request;
|
61 | this.response.__proto__ = parent.response;
|
62 | this.engines.__proto__ = parent.engines;
|
63 | this.settings.__proto__ = parent.settings;
|
64 | });
|
65 |
|
66 |
|
67 | this.locals = Object.create(null);
|
68 |
|
69 |
|
70 | this.mountpath = '/';
|
71 |
|
72 |
|
73 | this.locals.settings = this.settings;
|
74 |
|
75 |
|
76 | this.set('view', View);
|
77 | this.set('views', process.cwd() + '/views');
|
78 | this.set('jsonp callback name', 'callback');
|
79 |
|
80 | if (env === 'production') {
|
81 | this.enable('view cache');
|
82 | }
|
83 |
|
84 | Object.defineProperty(this, 'router', {
|
85 | get: function() {
|
86 | throw new Error('\'app.router\' is deprecated!\nPlease see the 3.x to 4.x migration guide for details on how to update your app.');
|
87 | }
|
88 | });
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | app.lazyrouter = function() {
|
100 | if (!this._router) {
|
101 | this._router = new Router({
|
102 | caseSensitive: this.enabled('case sensitive routing'),
|
103 | strict: this.enabled('strict routing')
|
104 | });
|
105 |
|
106 | this._router.use(query());
|
107 | this._router.use(middleware.init(this));
|
108 | }
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | app.handle = function(req, res, done) {
|
121 | var env = this.get('env');
|
122 |
|
123 | this._router.handle(req, res, function(err) {
|
124 | if (done) {
|
125 | return done(err);
|
126 | }
|
127 |
|
128 |
|
129 | if (err) {
|
130 |
|
131 | if (res.statusCode < 400) res.statusCode = 500;
|
132 | debug('default %s', res.statusCode);
|
133 |
|
134 |
|
135 | if (err.status) res.statusCode = err.status;
|
136 |
|
137 |
|
138 | var msg = 'production' == env
|
139 | ? http.STATUS_CODES[res.statusCode]
|
140 | : err.stack || err.toString();
|
141 | msg = escapeHtml(msg);
|
142 |
|
143 |
|
144 | if ('test' != env) console.error(err.stack || err.toString());
|
145 | if (res.headersSent) return req.socket.destroy();
|
146 | res.setHeader('Content-Type', 'text/html');
|
147 | res.setHeader('Content-Length', Buffer.byteLength(msg));
|
148 | if ('HEAD' == req.method) return res.end();
|
149 | res.end(msg);
|
150 | return;
|
151 | }
|
152 |
|
153 |
|
154 | debug('default 404');
|
155 | res.statusCode = 404;
|
156 | res.setHeader('Content-Type', 'text/html');
|
157 | if ('HEAD' == req.method) return res.end();
|
158 | res.end('Cannot ' + escapeHtml(req.method) + ' ' + escapeHtml(req.originalUrl) + '\n');
|
159 | });
|
160 | };
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | app.use = function(route, fn){
|
176 | var mount_app;
|
177 |
|
178 |
|
179 | if ('string' != typeof route) fn = route, route = '/';
|
180 |
|
181 |
|
182 | if (fn.handle && fn.set) mount_app = fn;
|
183 |
|
184 |
|
185 | if (mount_app) {
|
186 | debug('.use app under %s', route);
|
187 | mount_app.mountpath = route;
|
188 | fn = function(req, res, next) {
|
189 | var orig = req.app;
|
190 | mount_app.handle(req, res, function(err) {
|
191 | req.__proto__ = orig.request;
|
192 | res.__proto__ = orig.response;
|
193 | next(err);
|
194 | });
|
195 | };
|
196 | }
|
197 |
|
198 | this.lazyrouter();
|
199 | this._router.use(route, fn);
|
200 |
|
201 |
|
202 | if (mount_app) {
|
203 | mount_app.parent = this;
|
204 | mount_app.emit('mount', this);
|
205 | }
|
206 |
|
207 | return this;
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | app.route = function(path){
|
221 | this.lazyrouter();
|
222 | return this._router.route(path);
|
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 | app.engine = function(ext, fn){
|
260 | if ('function' != typeof fn) throw new Error('callback function required');
|
261 | if ('.' != ext[0]) ext = '.' + ext;
|
262 | this.engines[ext] = fn;
|
263 | return this;
|
264 | };
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | app.param = function(name, fn){
|
279 | var self = this;
|
280 | self.lazyrouter();
|
281 |
|
282 | if (Array.isArray(name)) {
|
283 | name.forEach(function(key) {
|
284 | self.param(key, fn);
|
285 | });
|
286 | return this;
|
287 | }
|
288 |
|
289 | self._router.param(name, fn);
|
290 | return this;
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | app.set = function(setting, val){
|
309 | if (1 == arguments.length) {
|
310 | return this.settings[setting];
|
311 | } else {
|
312 | this.settings[setting] = val;
|
313 |
|
314 | if (setting === 'trust proxy') {
|
315 | debug('compile trust proxy %j', val);
|
316 | this.set('trust proxy fn', compileTrust(val));
|
317 | }
|
318 |
|
319 | return this;
|
320 | }
|
321 | };
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | app.path = function(){
|
338 | return this.parent
|
339 | ? this.parent.path() + this.mountpath
|
340 | : '';
|
341 | };
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | app.enabled = function(setting){
|
359 | return !!this.set(setting);
|
360 | };
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | app.disabled = function(setting){
|
378 | return !this.set(setting);
|
379 | };
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | app.enable = function(setting){
|
390 | return this.set(setting, true);
|
391 | };
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | app.disable = function(setting){
|
402 | return this.set(setting, false);
|
403 | };
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | methods.forEach(function(method){
|
410 | app[method] = function(path){
|
411 | if (method === 'get' && arguments.length === 1) {
|
412 | return this.set(path);
|
413 | }
|
414 |
|
415 | var route = this._baseRoute(path);
|
416 | route[method].apply(route, [].slice.call(arguments, 1));
|
417 | return this;
|
418 | };
|
419 | });
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | app.all = function(path){
|
432 | var route = this._baseRoute(path);
|
433 | var args = [].slice.call(arguments, 1);
|
434 | methods.forEach(function(method){
|
435 | route[method].apply(route, args);
|
436 | });
|
437 |
|
438 | return this;
|
439 | };
|
440 |
|
441 |
|
442 |
|
443 | app.del = deprecate(app.delete, 'app.del: Use app.delete instead');
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 | app.render = function(name, options, fn){
|
463 | var opts = {};
|
464 | var cache = this.cache;
|
465 | var engines = this.engines;
|
466 | var view;
|
467 |
|
468 |
|
469 | if ('function' == typeof options) {
|
470 | fn = options, options = {};
|
471 | }
|
472 |
|
473 |
|
474 | mixin(opts, this.locals);
|
475 |
|
476 |
|
477 | if (options._locals) mixin(opts, options._locals);
|
478 |
|
479 |
|
480 | mixin(opts, options);
|
481 |
|
482 |
|
483 | opts.cache = null == opts.cache
|
484 | ? this.enabled('view cache')
|
485 | : opts.cache;
|
486 |
|
487 |
|
488 | if (opts.cache) view = cache[name];
|
489 |
|
490 |
|
491 | if (!view) {
|
492 | view = new (this.get('view'))(name, {
|
493 | defaultEngine: this.get('view engine'),
|
494 | root: this.get('views'),
|
495 | engines: engines
|
496 | });
|
497 |
|
498 | if (!view.path) {
|
499 | var err = new Error('Failed to lookup view "' + name + '" in views directory "' + view.root + '"');
|
500 | err.view = view;
|
501 | return fn(err);
|
502 | }
|
503 |
|
504 |
|
505 | if (opts.cache) cache[name] = view;
|
506 | }
|
507 |
|
508 |
|
509 | try {
|
510 | view.render(opts, fn);
|
511 | } catch (err) {
|
512 | fn(err);
|
513 | }
|
514 | };
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 | app.listen = function(){
|
538 | var server = http.createServer(this);
|
539 | return server.listen.apply(server, arguments);
|
540 | };
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 | app._baseRoute = function(path){
|
551 | this.lazyrouter();
|
552 |
|
553 | var key = typeof path !== 'string'
|
554 | ? 'o:' + String(path)
|
555 | : 's:' + path;
|
556 |
|
557 | if (path.ignoreCase || !this._router.caseSensitive) {
|
558 | key = 'i' + key.toLowerCase();
|
559 | }
|
560 |
|
561 | var route = this._baseRoutes[key];
|
562 |
|
563 | if (!route) {
|
564 | route = this._router.route(path);
|
565 | this._baseRoutes[key] = route;
|
566 | }
|
567 |
|
568 | return route;
|
569 | };
|