Skip to content
Myr

view.js

This file

Path

lib/view.js

205 lines · 3.7 KiB · JavaScript

Last changed by

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

History of this file

Take it

The walk stopped after 200 commits: lines older than 0ee4dd8 point at it.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 1–10why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */89'use strict';10

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 11–12why, one step back
11/**
12 * Module dependencies.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 13why, one step back
13 * @private

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 14–15why, one step back
14 */
15

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 16why, one step back
16var debug = require('debug')('express:view');

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 17–18why, one step back
17var path = require('path');
18var fs = require('fs');

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

lines 19–24why, one step back
19
20/**21 * Module variables.22 * @private23 */24

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 25–28why, one step back
25var dirname = path.dirname;
26var basename = path.basename;27var extname = path.extname;28var join = path.join;

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 29why, one step back
29var resolve = path.resolve;

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 30–31why, one step back
30
31/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 32–33why, one step back
32 * Module exports.
33 * @public

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 34–46why, one step back
34 */
3536module.exports = View;3738/**39 * Initialize a new `View` with the given `name`.40 *41 * Options:42 *43 * - `defaultEngine` the default template engine name44 * - `engines` template engine require() cache45 * - `root` root path for view lookup46 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 47–49why, one step back
47 * @param {string} name
48 * @param {object} options49 * @public

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 50–52why, one step back
50 */
5152function View(name, options) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 53–56why, one step back
53 var opts = options || {};
5455 this.defaultEngine = opts.defaultEngine;56 this.ext = extname(name);

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

line 57why, one step back
57 this.name = name;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 58–76why, one step back
58 this.root = opts.root;
5960 if (!this.ext && !this.defaultEngine) {61 throw new Error('No default engine was specified and no extension was provided.');62 }6364 var fileName = name;6566 if (!this.ext) {67 // get extension from default engine name68 this.ext = this.defaultEngine[0] !== '.'69 ? '.' + this.defaultEngine70 : this.defaultEngine;7172 fileName += this.ext;73 }7475 if (!opts.engines[this.ext]) {76 // load engine

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 77why, one step back
77 var mod = this.ext.slice(1)

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 78why, one step back
78 debug('require "%s"', mod)

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 79–87why, one step back
79
80 // default engine export81 var fn = require(mod).__express8283 if (typeof fn !== 'function') {84 throw new Error('Module "' + mod + '" does not provide a view engine.')85 }8687 opts.engines[this.ext] = fn

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 88–94why, one step back
88 }
8990 // store loaded engine91 this.engine = opts.engines[this.ext];9293 // lookup path94 this.path = this.lookup(fileName);

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 95–97why, one step back
95}
9697/**

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

line 98why, one step back
98 * Lookup view by the given `name`

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

line 99why, one step back
99 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 100–101why, one step back
100 * @param {string} name
101 * @private

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 102–103why, one step back
102 */
103

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 104–106why, one step back
104View.prototype.lookup = function lookup(name) {
105 var path;106 var roots = [].concat(this.root);

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

line 107why, one step back
107

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

line 108why, one step back
108 debug('lookup "%s"', name);

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 109why, one step back
109

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 110–111why, one step back
110 for (var i = 0; i < roots.length && !path; i++) {
111 var root = roots[i];

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 112why, one step back
112

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 113–116why, one step back
113 // resolve the path
114 var loc = resolve(root, name);115 var dir = dirname(loc);116 var file = basename(loc);

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

line 117why, one step back
117

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 118–122why, one step back
118 // resolve the file
119 path = this.resolve(dir, file);120 }121122 return path;

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 123–125why, one step back
123};
124125/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 126why, one step back
126 * Render with the given options.

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

line 127why, one step back
127 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 128–130why, one step back
128 * @param {object} options
129 * @param {function} callback130 * @private

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 131–132why, one step back
131 */
132

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 133why, one step back
133View.prototype.render = function render(options, callback) {

Make res.render callback is always async, even for sync view enginesDouglas Christopher Wilsonbefore adoption6343288

lines 134–135why, one step back
134 var sync = true;
135

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 136why, one step back
136 debug('render "%s"', this.path);

Make res.render callback is always async, even for sync view enginesDouglas Christopher Wilsonbefore adoption6343288

lines 137–158why, one step back
137
138 // render, normalizing sync callbacks139 this.engine(this.path, options, function onRender() {140 if (!sync) {141 return callback.apply(this, arguments);142 }143144 // copy arguments145 var args = new Array(arguments.length);146 var cntx = this;147148 for (var i = 0; i < arguments.length; i++) {149 args[i] = arguments[i];150 }151152 // force callback to be async153 return process.nextTick(function renderTick() {154 return callback.apply(cntx, args);155 });156 });157158 sync = false;

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

line 159why, one step back
159};

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 160–172why, one step back
160
161/**162 * Resolve the file within the given directory.163 *164 * @param {string} dir165 * @param {string} file166 * @private167 */168169View.prototype.resolve = function resolve(dir, file) {170 var ext = this.ext;171172 // <path>.<ext>

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 173–174why, one step back
173 var path = join(dir, file);
174 var stat = tryStat(path);

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 175–205why, one step back
175
176 if (stat && stat.isFile()) {177 return path;178 }179180 // <path>/index.<ext>181 path = join(dir, basename(file, ext), 'index' + ext);182 stat = tryStat(path);183184 if (stat && stat.isFile()) {185 return path;186 }187};188189/**190 * Return a stat, maybe.191 *192 * @param {string} path193 * @return {fs.Stats}194 * @private195 */196197function tryStat(path) {198 debug('stat "%s"', path);199200 try {201 return fs.statSync(path);202 } catch (e) {203 return undefined;204 }205}