view.js
This file
Path
lib/view.js
173 lines · 3.1 KiB · JavaScript
Last changed by
perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: add license commentsDouglas Christopher Wilsonbefore adoption95ad276
lines 1–8why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f
lines 9–10why, one step back9'use strict';10docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 11–12why, one step back11/**12 * Module dependencies.perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 13why, one step back13 * @privatedocs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 14–15why, one step back14 */15Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 16why, one step back16var debug = require('debug')('express:view');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 17–19why, one step back17var path = require('path');18var fs = require('fs');19var utils = require('./utils');Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
lines 20–25why, one step back2021/**22 * Module variables.23 * @private24 */25docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 26–29why, one step back26var dirname = path.dirname;27var basename = path.basename;28var extname = path.extname;29var join = path.join;Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 30why, one step back30var resolve = path.resolve;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 31–32why, one step back3132/**perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 33–34why, one step back33 * Module exports.34 * @publicdocs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 35–47why, one step back35 */3637module.exports = View;3839/**40 * Initialize a new `View` with the given `name`.41 *42 * Options:43 *44 * - `defaultEngine` the default template engine name45 * - `engines` template engine require() cache46 * - `root` root path for view lookup47 *perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 48–50why, one step back48 * @param {string} name49 * @param {object} options50 * @publicdocs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 51–53why, one step back51 */5253function View(name, options) {perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 54–57why, one step back54 var opts = options || {};5556 this.defaultEngine = opts.defaultEngine;57 this.ext = extname(name);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 58why, one step back58 this.name = name;perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 59–85why, one step back59 this.root = opts.root;6061 if (!this.ext && !this.defaultEngine) {62 throw new Error('No default engine was specified and no extension was provided.');63 }6465 var fileName = name;6667 if (!this.ext) {68 // get extension from default engine name69 this.ext = this.defaultEngine[0] !== '.'70 ? '.' + this.defaultEngine71 : this.defaultEngine;7273 fileName += this.ext;74 }7576 if (!opts.engines[this.ext]) {77 // load engine78 opts.engines[this.ext] = require(this.ext.substr(1)).__express;79 }8081 // store loaded engine82 this.engine = opts.engines[this.ext];8384 // lookup path85 this.path = this.lookup(fileName);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 86–88why, one step back86}8788/**Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 89why, one step back89 * Lookup view by the given `name`docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 90why, one step back90 *perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 91–92why, one step back91 * @param {string} name92 * @privatedocs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 93–94why, one step back93 */94Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 95–97why, one step back95View.prototype.lookup = function lookup(name) {96 var path;97 var roots = [].concat(this.root);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 98why, one step back98Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 99why, one step back99 debug('lookup "%s"', name);Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 100why, one step back100Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 101–102why, one step back101 for (var i = 0; i < roots.length && !path; i++) {102 var root = roots[i];Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 103why, one step back103Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 104–107why, one step back104 // resolve the path105 var loc = resolve(root, name);106 var dir = dirname(loc);107 var file = basename(loc);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 108why, one step back108Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 109–113why, one step back109 // resolve the file110 path = this.resolve(dir, file);111 }112113 return path;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 114–116why, one step back114};115116/**perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 117why, one step back117 * Render with the given options.docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 118why, one step back118 *perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 119–121why, one step back119 * @param {object} options120 * @param {function} callback121 * @privatedocs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 122–123why, one step back122 */123perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 124why, one step back124View.prototype.render = function render(options, callback) {Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8
line 125why, one step back125 debug('render "%s"', this.path);perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
line 126why, one step back126 this.engine(this.path, options, callback);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 127why, one step back127};Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 128–140why, one step back128129/**130 * Resolve the file within the given directory.131 *132 * @param {string} dir133 * @param {string} file134 * @private135 */136137View.prototype.resolve = function resolve(dir, file) {138 var ext = this.ext;139140 // <path>.<ext>perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564
lines 141–142why, one step back141 var path = join(dir, file);142 var stat = tryStat(path);Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 143–173why, one step back143144 if (stat && stat.isFile()) {145 return path;146 }147148 // <path>/index.<ext>149 path = join(dir, basename(file, ext), 'index' + ext);150 stat = tryStat(path);151152 if (stat && stat.isFile()) {153 return path;154 }155};156157/**158 * Return a stat, maybe.159 *160 * @param {string} path161 * @return {fs.Stats}162 * @private163 */164165function tryStat(path) {166 debug('stat "%s"', path);167168 try {169 return fs.statSync(path);170 } catch (e) {171 return undefined;172 }173}