nodejs express 框架解密2-如何创建一个app
本文是基于express 3.4.6 的
1.在我们的app.js 文件里面有这么几行
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
这个其实是调用http模块 的 createServer 函数创建一个服务,然后监听端口的。
2. 我们再去看看express 的入口文件
/**
* Module dependencies.
*/ var connect = require('connect')
, proto = require('./application')
, Route = require('./router/route')
, Router = require('./router')
, req = require('./request')
, res = require('./response')
, utils = connect.utils; /**
* Expose `createApplication()`.
*/ exports = module.exports = createApplication; /**
* Expose mime.
*/ exports.mime = connect.mime; /**
* Create an express application.
*
* @return {Function}
* @api public
*/ function createApplication() {
var app = connect();
//将application中的方法全部拷贝到connect对象上去。
utils.merge(app, proto);
//设置app 的request对象的原型为req,本身的属性为connect对象
app.request = { __proto__: req, app: app };
//设置app的response对象原型为res ,本身的属性为connect对象
app.response = { __proto__: res, app: app };
//调用application中的方法init
app.init();
return app;
} /**
* Expose connect.middleware as express.*
* for example `express.logger` etc.
*/
/**
* 加载connect模块中得所有中间件
*/
for (var key in connect.middleware) {
Object.defineProperty(
exports
, key
, Object.getOwnPropertyDescriptor(connect.middleware, key));
} /**
* Error on createServer().
*/
/**
* 将创建服务器的方法输出
* @returns {Function}
*/
exports.createServer = function(){
console.warn('Warning: express.createServer() is deprecated, express');
console.warn('applications no longer inherit from http.Server,');
console.warn('please use:');
console.warn('');
console.warn(' var express = require("express");');
console.warn(' var app = express();');
console.warn('');
//加载创建应用程序的方法,开始创建application
return createApplication();
}; /**
* Expose the prototypes.
*/ exports.application = proto;
exports.request = req;
exports.response = res; /**
* Expose constructors.
*/ exports.Route = Route;
exports.Router = Router; // Error handler title exports.errorHandler.title = 'Express';
可以看到exports = module.exports = createApplication;将这个作为模块导出了,作为一个构造函数。
这个函数是:
function createApplication() {
var app = connect();
//将application中的方法全部拷贝到connect对象上去。
utils.merge(app, proto);
//设置app 的request对象的原型为req,本身的属性为connect对象
app.request = { __proto__: req, app: app };
//设置app的response对象原型为res ,本身的属性为connect对象
app.response = { __proto__: res, app: app };
//调用application中的方法init
app.init();
return app;
}
首先调用connect 组件app,于是将proto 上该有的方法都拷贝到app上去。proto是神马么?它就是 proto = require('./application') application.js 输出的“app” 对象 所有得函数,
接着将req,res 作为 组件app 的request,response 的原型,同时将app作为他们的一个属性,为什么要这么做呢?后面就会看到。最后调用app.init()方法,这个其实是调用application
中的init方法。
3.application.js
app.init = function(){
this.cache = {};
this.settings = {};
this.engines = {};
//默认配置
this.defaultConfiguration();
};
我们看到他是直接调用defaultConfiguration 方法的。我们再去看看defaultConfiguration方法的实现
app.defaultConfiguration = function(){
// default settings
this.enable('x-powered-by');
this.enable('etag');
this.set('env', process.env.NODE_ENV || 'development');
this.set('subdomain offset', 2);
debug('booting in %s mode', this.get('env'));
// implicit middleware
//调用中间件
this.use(connect.query());
this.use(middleware.init(this));
// inherit protos
//继承原型
this.on('mount', function(parent){
this.request.__proto__ = parent.request;
this.response.__proto__ = parent.response;
this.engines.__proto__ = parent.engines;
this.settings.__proto__ = parent.settings;
});
//router
//路由
this._router = new Router(this);
this.routes = this._router.map;
this.__defineGetter__('router', function(){
this._usedRouter = true;
this._router.caseSensitive = this.enabled('case sensitive routing');
this._router.strict = this.enabled('strict routing');
return this._router.middleware;
});
// setup locals
this.locals = locals(this);
// default locals
this.locals.settings = this.settings;
// default configuration
this.set('view', View);
this.set('views', process.cwd() + '/views');
this.set('jsonp callback name', 'callback');
this.configure('development', function(){
this.set('json spaces', 2);
});
this.configure('production', function(){
this.enable('view cache');
});
};
从代码中可以看到,它首先调用中间件,中间件的作用主要是改写改写request,response 请求的。将这2个请求导出,方便后面的模板渲染。然后再调用路由模块。路由模块只要是根据path
调用路由分发函数分发路由,执行callback,最后调用view 模块,渲染我们的模板。
nodejs express 框架解密2-如何创建一个app的更多相关文章
- nodejs express 框架解密4-路由
本文档是基于express3.4.6 express 的路由是自己去实现的,没有使用connect中的路由中间件模块. 1.在如何创建一个app那篇中,我们提到了路由, //router //路由 t ...
- nodejs express 框架解密1-总体结构
本文是基于express3.4.6的. 1.express 代码结构为: bin/express 是在命令行下的生成express 框架目录文件用的 lib/express 是框架的入口文件 lib/ ...
- nodejs express 框架解密3-中间件模块
本文档是基于express 3.4.6 的 在上篇中我们提到了中间件,这篇主要解释这个模块,middleware.js 为: var utils = require('./utils'); /** * ...
- nodejs express 框架解密5-视图
本文档是基于express 3.4.6 的 在我们的代码中,渲染模板大致是这样写的 exports.index = function(req, res){ res.render('index', { ...
- Express 的基本使用(创建一个简单的服务器)
Express 的基本使用(创建一个简单的服务器) const express = require('express') // 创建服务器应用程序 // 相当于 http.creatServer co ...
- diango创建一个app
创建一个app terminal里执行命令 python manage.py startapp app名称 注册 settings配置 INSTALLED_APPS = [ 'app01', 'app ...
- nodeJS express框架 中文乱码解决办法
最近在研究javascript 的服务端应用 node,之所以想要研究node,是因为前几个月一直在前端挣扎,从javascript入门到在项目中实际使用javascript,确实感悟颇深.javas ...
- React第一篇: 搭建React + nodejs + express框架
前提: 需要安装Node.js (>6)版本 1.cmd进到本地某个目录, 逐行输入以下指令(以下括号为注释) npm install -g create-react-app (全局安装cr ...
- NodeJS express框架的使用
首先,可以通过npm或者淘宝镜像cnpm全局安装epress框架,这里不具体说了 npm install -g expressnpm install -g express-generator 新建一个 ...
随机推荐
- tar 压缩去除目录
tar zcvf fd.tar.gz * --exclude=file1 --exclude=dir1 注意: 1.yes: --exclude=file1 no: --exclude file1 ...
- shell语法快速入门(1)
#得到绝对路径 DIR=$(cd `dirname $0`;pwd) $DIR/file.txt #去掉#注释 egrep -v "(#|^$)" /etc/zabbix/zabb ...
- Linux更改服务器Hostname
在我们需要维护较多的服务器时,有意义的Hostname将时刻提醒我们这台服务器的功能. ****** 1.Debian echo '127.0.1.1 git-server' >> /et ...
- C++ 优先队列
C++ 优先队列 #include <queue> priority_queue<Type, Container, Functional>:Type为数据的类型,Contain ...
- 使用NHibernate(9)-- 缓存
1,对象状态. 作为基础,还是先看一下对象的状态吧.主要涉及到三个名词,瞬时.持久.托管. 瞬时态:对象刚创建,Session还不知道这个对象的存在.可以通过调用ISession的Save等方法可以转 ...
- 大熊君说说JS与设计模式之------策略模式Strategy
一,总体概要 1,笔者浅谈 策略模式,又叫算法簇模式,就是定义了不同的算法,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式和工厂模式有一定的类似,策略模式相对简单容易理解,并 ...
- 中文版Windows Server 2012 R2更改为英文显示语言
1. 下载Windows Server 2012 R2多语言包(下载页面,直接下载链接). 2. 将win_svr_2012_r2_64bit_multi_language_lp_oem.img解压或 ...
- 学习WPF——元素绑定
概念 从源对象提取一些信息,并用这些信息设置目标对象的属性 示例 在给TextBlock控件的FontSize属性赋值时,我们使用了绑定表达式 数据绑定表达式使用XAML的标记扩展(因此具有花括号)( ...
- Mac下安装zshell
Mac 下安装zshell 什么是shell 大多数命令行用户接触最多的是Bash,因为Bash是各个版本操作系统(Linux&Mac)的默认shell. 查看当前使用的shell $ ech ...
- 转载:APP的上线和推广——线上推广渠道
本文版权归个人所有,如需转载请注明出处http://www.cnblogs.com/PengLee/p/4637080.html 目录 应用商店 互联网开放平台 软件下载中心 媒体社交平台 刷榜推广 ...