本文档是基于express 3.4.6 的

在我们的代码中,渲染模板大致是这样写的

exports.index = function(req, res){
res.render('index', { title: 'Express' });
};

这个req,res 函数其实是经过了中间件middleware.js 处理后的,我们在前面提到过。

req,res的原型分别为 app.request 和app.response

req.__proto__ = app.request;
res.__proto__ = app.response;

而 app.request 和app.response,本身也是具有app的属性的。

//设置app 的request对象的原型为req,本身的属性为connect对象
app.request = { __proto__: req, app: app };
//设置app的response对象原型为res ,本身的属性为connect对象
app.response = { __proto__: res, app: app };

注意这里的这个app属性

打开response.js 文件,我们看看res.render方法

res.render = function(view, options, fn){
var self = this
, options = options || {}
, req = this.req
, app = req.app; // support callback function as second arg
if ('function' == typeof options) {
fn = options, options = {};
} // merge res.locals
options._locals = self.locals; // default callback to respond
fn = fn || function(err, str){
if (err) return req.next(err);
self.send(str);
}; // render
app.render(view, options, fn);
};

可以看到:

, req = this.req
, app = req.app;

这里的 req = this.req,其实是中间件 里面的  res.req = req;

第二句直接将app带进来了,最后我们执行了app.render方法,它调用了application.js 中得这个方法:

app.render = function(name, options, fn){
var opts = {}
, cache = this.cache
, engines = this.engines
, view; // support callback function as second arg
if ('function' == typeof options) {
fn = options, options = {};
} // merge app.locals
utils.merge(opts, this.locals); // merge options._locals
if (options._locals) utils.merge(opts, options._locals); // merge options
utils.merge(opts, options); // set .cache unless explicitly provided
opts.cache = null == opts.cache
? this.enabled('view cache')
: opts.cache; // primed cache
if (opts.cache) view = cache[name]; // view
if (!view) {
view = new (this.get('view'))(name, {
defaultEngine: this.get('view engine'),
root: this.get('views'),
engines: engines
}); if (!view.path) {
var err = new Error('Failed to lookup view "' + name + '"');
err.view = view;
return fn(err);
} // prime the cache
if (opts.cache) cache[name] = view;
} // render
try {
view.render(opts, fn);
} catch (err) {
fn(err);
}
};

可以看到它调用了视图类:

 view = new (this.get('view'))(name, {
defaultEngine: this.get('view engine'),
root: this.get('views'),
engines: engines
});

最后调用了视图的render方法。 view.render(opts, fn);

视图的这个方法是:

View.prototype.render = function(options, fn){
this.engine(this.path, options, fn);
};
function View(name, options) {
options = options || {};
this.name = name;
this.root = options.root;
var engines = options.engines;
this.defaultEngine = options.defaultEngine;
var ext = this.ext = extname(name);
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);
this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);
this.path = this.lookup(name);
}

引擎require了一个方法

this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);

后面就去调用具体的模板引擎了。

nodejs express 框架解密5-视图的更多相关文章

  1. nodejs express 框架解密1-总体结构

    本文是基于express3.4.6的. 1.express 代码结构为: bin/express 是在命令行下的生成express 框架目录文件用的 lib/express 是框架的入口文件 lib/ ...

  2. nodejs express 框架解密3-中间件模块

    本文档是基于express 3.4.6 的 在上篇中我们提到了中间件,这篇主要解释这个模块,middleware.js 为: var utils = require('./utils'); /** * ...

  3. nodejs express 框架解密4-路由

    本文档是基于express3.4.6 express 的路由是自己去实现的,没有使用connect中的路由中间件模块. 1.在如何创建一个app那篇中,我们提到了路由, //router //路由 t ...

  4. nodejs express 框架解密2-如何创建一个app

    本文是基于express 3.4.6 的 1.在我们的app.js 文件里面有这么几行 http.createServer(app).listen(app.get('port'), function( ...

  5. React第一篇: 搭建React + nodejs + express框架

    前提: 需要安装Node.js (>6)版本 1.cmd进到本地某个目录, 逐行输入以下指令(以下括号为注释) npm install -g create-react-app   (全局安装cr ...

  6. nodeJS express框架 中文乱码解决办法

    最近在研究javascript 的服务端应用 node,之所以想要研究node,是因为前几个月一直在前端挣扎,从javascript入门到在项目中实际使用javascript,确实感悟颇深.javas ...

  7. NodeJS express框架的使用

    首先,可以通过npm或者淘宝镜像cnpm全局安装epress框架,这里不具体说了 npm install -g expressnpm install -g express-generator 新建一个 ...

  8. nodejs express 框架 上传文件

    web 项目应用express4.0框架 html 表单post 文件上传失败,后端无法获取提交文件 express不支持文件上传. 方式一 若是图片,可以将图片转码为BASE64上传 前端框架ang ...

  9. 安装nodejs express框架时express命令行无效

    我也是看了这篇才明白.http://jingyan.baidu.com/article/922554468a3466851648f419.html 最近在看一本书,nodejs开发指南.至于出现这个问 ...

随机推荐

  1. js-特效部分学习-拖拽效果

    一.客户区大小ClientWidth和ClientHeight <style> #box { width: 200px; height: 200px; background-color: ...

  2. js-BOM之offset家族、移动函数的封装升级(轮播图)

    Obj.style.width/obj.style.height与obj.offsetWidth/obj.offsetHeight的区别: <style> #div1{ height: 2 ...

  3. MySQL MHA配置

    MySQL环境: master:192.168.202.129:3306 slave:192.168.202.129:3307,192.168.202.129:3307,192.168.202.130 ...

  4. MySQL设置字符集CHARACTER SET

    本文地址:http://www.cnblogs.com/yhLinux/p/4036506.html 在 my.cnf 配置文件中设置相关选项,改变为相应的character set. 设置数据库编码 ...

  5. 20151010 C# 第一篇 变量类型

    20151010 变量类型: 1. 值类型:变量本身直接存储数据 整数类型:代表没有小数点的整数数值 类型 说明 范围 sbyte 8位有符号整数 -128——127 short 16位有符号整数 - ...

  6. 今天开始着手原来Office系统的重构

    原来系统架构Spring+Hibernate+Struts+springsecurity 拟改成 Spring+SpringMVC+MyBatis/JDBC+Shiro 同时优化前端的CSS和JQue ...

  7. 2015年8月18日,杨学明老师《技术部门的绩效管理提升(研讨会)》在中国科学院下属机构CNNIC成功举办!

    2015年8月18日,杨学明老师为中国网络新闻办公室直属央企中国互联网络中心(CNNIC)提供了一天的<技术部门的绩效管理提升(研讨会)>培训课程.杨学明老师分别从研发绩效管理概述.研发绩 ...

  8. 对于C++窗口编译一闪而过的解决方法 (DEV CPP下)

    对于C++窗口编译一闪而过的解决方法 首先来看一个简单的程序(编译环境为 DEV C++.):  #include <iostream>  int main()  {      std:: ...

  9. Delete characters

    Description In this exercise, you will get two strings A and B in each test group and the length of ...

  10. 关闭Ubuntu 12.04的内部错误提示

    刚装完系统后,才安装一个输入法重启电脑后,竟然就提示'内部错误'需要提交报告,什么状况? 发扬'不求甚解'的光荣传统,我又不搞Linux开发,对我来说只是个工具而已,工具出问题了解决问题即可不想劳神深 ...