项目简介
这是一个资产管理项目,主要的目的就是实现对资产的无纸化管理。通过为每个资产生成二维码,来联合移动终端完成对资产的审核等。这个项目既提供了Web端的管理界面也提供移动端(Andorid)的资产审核、派发等相关功能。
我们用Node.js构建该项目的Web端以及移动端的Serveice API。
项目主框架:Express 简介
Express 是一个非常流行的node.js的web框架。基于connect(node中间件框架)。提供了很多便于处理http请求等web开发相关的扩展。
<
http://v.17173.com/playlist_14563960.htmlhttp://v.17173.com/playlist_14564303.htmlhttp://v.17173.com/playlist_14564506.htmlhttp://v.17173.com/playlist_14564744.htmlhttp://v.17173.com/playlist_14564831.htmlhttp://v.17173.com/playlist_14565260.htmlhttp://v.17173.com/playlist_14565579.htmlhttp://v.17173.com/playlist_14565683.htmlhttp://v.17173.com/playlist_14565735.htmlhttp://v.17173.com/playlist_14565925.htmlhttp://v.17173.com/playlist_14566003.htmlhttp://v.17173.com/playlist_14566321.htmlhttp://v.17173.com/playlist_14566552.htmlhttp://v.17173.com/playlist_14566645.htmlhttp://v.17173.com/playlist_14566776.htmlhttp://v.17173.com/playlist_14566874.htmlhttp://v.17173.com/playlist_14567138.htmlhttp://v.17173.com/playlist_14567348.htmlhttp://v.17173.com/playlist_14567498.htmlhttp://v.17173.com/playlist_14567661.htmlhttp://v.17173.com/playlist_14568038.htmlhttp://v.17173.com/playlist_14568121.htmlhttp://v.17173.com/playlist_14568701.htmlhttp://v.17173.com/playlist_14568926.htmlhttp://v.17173.com/playlist_14569140.htmlhttp://v.17173.com/playlist_14569273.htmlhttp://v.17173.com/playlist_14569630.htmlhttp://v.17173.com/playlist_14569687.htmlhttp://v.17173.com/playlist_14569851.htmlhttp://v.17173.com/playlist_14569953.htmlhttp://v.17173.com/playlist_14570045.htmlhttp://v.17173.com/playlist_14570109.htmlhttp://v.17173.com/playlist_14570193.htmlhttp://v.17173.com/playlist_14570335.htmlhttp://v.17173.com/playlist_14570422.htmlhttp://v.17173.com/playlist_14570508.htmlhttp://v.17173.com/playlist_14570572.htmlhttp://v.17173.com/playlist_14570686.htmlhttp://v.17173.com/playlist_14570807.htmlhttp://v.17173.com/playlist_14571026.htmlhttp://v.17173.com/playlist_14571622.htmlhttp://v.17173.com/playlist_14572020.htmlhttp://v.17173.com/playlist_14572383.htmlhttp://v.17173.com/playlist_14572492.htmlhttp://v.17173.com/playlist_14572585.htmlhttp://v.17173.com/playlist_14572661.htmlhttp://v.17173.com/playlist_14572841.htmlhttp://v.17173.com/playlist_14572937.htmlhttp://v.17173.com/playlist_14573043.htmlhttp://v.17173.com/playlist_14573116.htmlhttp://v.17173.com/playlist_14573299.htmlhttp://v.17173.com/playlist_14573415.htmlhttp://v.17173.com/playlist_14573840.htmlhttp://v.17173.com/playlist_14573969.htmlhttp://v.17173.com/playlist_14574041.htmlhttp://v.17173.com/playlist_14574204.htmlhttp://v.17173.com/playlist_14574305.htmlhttp://v.17173.com/playlist_14574434.htmlhttp://v.17173.com/playlist_14574517.htmlhttp://v.17173.com/playlist_14574889.htmlhttp://v.17173.com/playlist_14574949.htmlhttp://v.17173.com/playlist_14575031.htmlhttp://v.17173.com/playlist_14575355.htmlhttp://v.17173.com/playlist_14575420.htmlhttp://v.17173.com/playlist_14575607.htmlhttp://v.17173.com/playlist_14575776.htmlhttp://v.17173.com/playlist_14575873.htmlhttp://v.17173.com/playlist_14576031.htmlhttp://v.17173.com/playlist_14576273.htmlhttp://v.17173.com/playlist_14576490.htmlhttp://v.17173.com/playlist_14576623.htmlhttp://v.17173.com/playlist_14576706.htmlhttp://v.17173.com/playlist_14576899.htmlhttp://v.17173.com/playlist_14577008.htmlhttp://v.17173.com/playlist_14577072.htmlhttp://v.17173.com/playlist_14577150.htmlhttp://v.17173.com/playlist_14577225.htmlhttp://v.17173.com/playlist_14577339.htmlhttp://v.17173.com/playlist_14577403.htmlhttp://v.17173.com/playlist_14577776.htmlhttp://v.17173.com/playlist_14577889.htmlhttp://v.17173.com/playlist_14577956.htmlhttp://v.17173.com/playlist_14578173.htmlhttp://v.17173.com/playlist_14578331.htmlhttp://v.17173.com/playlist_14578385.htmlhttp://v.17173.com/playlist_14578633.htmlhttp://v.17173.com/playlist_14578726.htmlhttp://v.17173.com/playlist_14578815.htmlhttp://v.17173.com/playlist_14578975.htmlhttp://v.17173.com/playlist_14579092.htmlhttp://v.17173.com/playlist_14579248.htmlhttp://v.17173.com/playlist_14579586.htmlhttp://v.17173.com/playlist_14579958.htmlhttp://v.17173.com/playlist_14580196.htmlhttp://v.17173.com/playlist_14580278.htmlhttp://v.17173.com/playlist_14580392.htmlhttp://v.17173.com/playlist_14580472.htmlhttp://v.17173.com/playlist_14580587.htmlhttp://v.17173.com/playlist_14580732.htmlhttp://v.17173.com/playlist_14580997.htmlhttp://v.17173.com/playlist_14581074.htmlhttp://v.17173.com/playlist_14581151.htmlhttp://v.17173.com/playlist_14581232.htmlhttp://v.17173.com/playlist_14581509.htmlhttp://v.17173.com/playlist_14581859.htmlhttp://v.17173.com/playlist_14581920.htmlhttp://v.17173.com/playlist_14582011.htmlhttp://v.17173.com/playlist_14582099.htmlhttp://v.17173.com/playlist_14582202.htmlhttp://v.17173.com/playlist_14582294.htmlhttp://v.17173.com/playlist_14582421.htmlhttp://v.17173.com/playlist_14582546.htmlhttp://v.17173.com/playlist_14582642.htmlhttp://v.17173.com/playlist_14582724.htmlhttp://v.17173.com/playlist_14582796.htmlhttp://v.17173.com/playlist_14582912.htmlhttp://v.17173.com/playlist_14583017.htmlhttp://v.17173.com/playlist_14589127.htmlhttp://v.17173.com/playlist_14589288.htmlhttp://v.17173.com/playlist_14589391.htmlhttp://v.17173.com/playlist_14589705.htmlhttp://v.17173.com/playlist_14589927.htmlhttp://v.17173.com/playlist_14590798.htmlhttp://v.17173.com/playlist_14590888.htmlhttp://v.17173.com/playlist_14590972.htmlhttp://v.17173.com/playlist_14591044.htmlhttp://v.17173.com/playlist_14591105.htmlhttp://v.17173.com/playlist_14591182.htmlhttp://v.17173.com/playlist_14591308.htmlhttp://v.17173.com/playlist_14591369.htmlhttp://v.17173.com/playlist_14591708.htmlhttp://v.17173.com/playlist_14591767.htmlhttp://v.17173.com/playlist_14591972.htmlhttp://v.17173.com/playlist_14592053.htmlhttp://v.17173.com/playlist_14592121.htmlhttp://v.17173.com/playlist_14592289.htmlhttp://v.17173.com/playlist_14592411.htmlhttp://v.17173.com/playlist_14592546.htmlhttp://v.17173.com/playlist_14592611.htmlhttp://v.17173.com/playlist_14592857.htmlhttp://v.17173.com/playlist_14592944.htmlhttp://v.17173.com/playlist_14593268.htmlhttp://v.17173.com/playlist_14593503.htmlhttp://v.17173.com/playlist_14593556.htmlhttp://v.17173.com/playlist_14593855.htmlhttp://v.17173.com/playlist_14594206.htmlhttp://v.17173.com/playlist_14594323.htmlhttp://v.17173.com/playlist_14594398.htmlhttp://v.17173.com/playlist_14594461.htmlhttp://v.17173.com/playlist_14594587.htmlhttp://v.17173.com/playlist_14594735.htmlhttp://v.17173.com/playlist_14594805.htmlhttp://v.17173.com/playlist_14594893.htmlhttp://v.17173.com/playlist_14595011.htmlhttp://v.17173.com/playlist_14595091.htmlhttp://v.17173.com/playlist_14595165.htmlhttp://v.17173.com/playlist_14595466.htmlhttp://v.17173.com/playlist_14595662.htmlhttp://v.17173.com/playlist_14595866.htmlhttp://v.17173.com/playlist_14595921.htmlhttp://v.17173.com/playlist_14596083.htmlhttp://v.17173.com/playlist_14596354.htmlhttp://v.17173.com/playlist_14596441.htmlhttp://v.17173.com/playlist_14596585.htmlhttp://v.17173.com/playlist_14596645.htmlhttp://v.17173.com/playlist_14596698.htmlhttp://v.17173.com/playlist_14597078.htmlhttp://v.17173.com/playlist_14597141.htmlhttp://v.17173.com/playlist_14597199.htmlhttp://v.17173.com/playlist_14597260.htmlhttp://v.17173.com/playlist_14597350.htmlhttp://v.17173.com/playlist_14597750.htmlhttp://v.17173.com/playlist_14597822.htmlhttp://v.17173.com/playlist_14597892.htmlhttp://v.17173.com/playlist_14597991.htmlhttp://v.17173.com/playlist_14598088.htmlhttp://v.17173.com/playlist_14598170.htmlhttp://v.17173.com/playlist_14598275.htmlhttp://v.17173.com/playlist_14598396.htmlhttp://v.17173.com/playlist_14598455.htmlhttp://v.17173.com/playlist_14598508.htmlhttp://v.17173.com/playlist_14598569.htmlhttp://v.17173.com/playlist_14598628.htmlhttp://v.17173.com/playlist_14598697.htmlhttp://v.17173.com/playlist_14598765.htmlhttp://v.17173.com/playlist_14598965.htmlhttp://v.17173.com/playlist_14599285.htmlhttp://v.17173.com/playlist_14599344.htmlhttp://v.17173.com/playlist_14599484.htmlhttp://v.17173.com/playlist_14599582.htmlhttp://v.17173.com/playlist_14599623.htmlhttp://v.17173.com/playlist_14599715.htmlhttp://v.17173.com/playlist_14599795.htmlhttp://v.17173.com/playlist_14599862.htmlhttp://v.17173.com/playlist_14599916.htmlhttp://v.17173.com/playlist_14599982.htmlhttp://v.17173.com/playlist_14600072.htmlhttp://v.17173.com/playlist_14600167.htmlhttp://v.17173.com/playlist_14600229.htmlhttp://v.17173.com/playlist_14600273.htmlhttp://blog.e-works.net.cn/681655/articles/849291.htmlhttp://blog.e-works.net.cn/681655/articles/849289.htmlhttp://blog.e-works.net.cn/681655/articles/849287.htmlhttp://blog.e-works.net.cn/681655/articles/849285.htmlhttp://blog.e-works.net.cn/681655/articles/849278.htmlhttp://blog.e-works.net.cn/681655/articles/849244.htmlhttp://blog.e-works.net.cn/681655/articles/849225.htmlhttp://blog.e-works.net.cn/681655/articles/849224.htmlhttp://blog.e-works.net.cn/681655/articles/849223.htmlhttp://blog.e-works.net.cn/681655/articles/849222.htmlhttp://blog.e-works.net.cn/681655/articles/849221.htmlhttp://blog.e-works.net.cn/681655/articles/849219.htmlhttp://blog.e-works.net.cn/681655/articles/849217.htmlhttp://blog.e-works.net.cn/681655/articles/849040.htmlhttp://blog.e-works.net.cn/681655/articles/849035.htmlhttp://blog.e-works.net.cn/681655/articles/849030.htmlhttp://blog.e-works.net.cn/681655/articles/849027.htmlhttp://blog.e-works.net.cn/681655/articles/849018.htmlhttp://blog.e-works.net.cn/681655/articles/848973.htmlhttp://blog.e-works.net.cn/681655/articles/848968.htmlhttp://blog.e-works.net.cn/681655/articles/848964.htmlhttp://blog.e-works.net.cn/681655/articles/848960.htmlhttp://blog.e-works.net.cn/681655/articles/848958.htmlhttp://blog.e-works.net.cn/681655/articles/848953.htmlhttp://blog.e-works.net.cn/681655/articles/848948.htmlhttp://blog.e-works.net.cn/681655/articles/848943.htmlhttp://blog.e-works.net.cn/681655/articles/848941.htmlhttp://blog.e-works.net.cn/681655/articles/848935.htmlhttp://blog.e-works.net.cn/681655/articles/848932.htmlhttp://blog.e-works.net.cn/681655/articles/848924.htmlhttp://blog.e-works.net.cn/681655/articles/848917.htmlhttp://blog.e-works.net.cn/681655/articles/848906.htmlhttp://blog.e-works.net.cn/681655/articles/848903.htmlhttp://blog.e-works.net.cn/681655/articles/848901.htmlhttp://blog.e-works.net.cn/681655/articles/848898.htmlhttp://blog.e-works.net.cn/681655/articles/848893.htmlhttp://blog.e-works.net.cn/681655/articles/848888.htmlhttp://blog.e-works.net.cn/681655/articles/848884.htmlhttp://blog.e-works.net.cn/681655/articles/848881.htmlhttp://blog.e-works.net.cn/681655/articles/848878.htmlhttp://blog.e-works.net.cn/681655/articles/848876.htmlhttp://blog.e-works.net.cn/681655/articles/848868.htmlhttp://blog.e-works.net.cn/681655/articles/848865.htmlhttp://blog.e-works.net.cn/681655/articles/848871.htmlhttp://www.huihui.cn/share/28829904http://www.huihui.cn/share/27666750http://www.huihui.cn/share/28833212http://www.huihui.cn/share/28839722http://www.huihui.cn/share/28841742http://www.huihui.cn/share/28843064http://www.huihui.cn/share/28844304http://www.huihui.cn/share/28845758http://www.huihui.cn/share/28847054http://www.huihui.cn/share/28850212http://www.huihui.cn/share/28851674http://www.huihui.cn/share/28856144http://www.huihui.cn/share/28858024http://www.huihui.cn/share/28861758http://www.huihui.cn/share/28863048http://www.huihui.cn/share/28864418http://www.huihui.cn/share/28866132http://www.huihui.cn/share/28867714http://www.huihui.cn/share/28870046http://www.huihui.cn/share/28871760http://www.huihui.cn/share/28873206http://www.huihui.cn/share/28874210http://www.huihui.cn/share/28875156http://www.huihui.cn/share/28877132http://www.huihui.cn/share/28878258http://www.huihui.cn/share/28879368http://www.huihui.cn/share/28880406http://www.huihui.cn/share/28884024http://www.huihui.cn/share/28885128http://www.huihui.cn/share/28886472http://www.huihui.cn/share/28887718http://www.huihui.cn/share/28889156http://www.huihui.cn/share/28890864http://www.huihui.cn/share/28891888http://www.huihui.cn/share/28893078http://www.huihui.cn/share/28894388http://www.huihui.cn/share/28895284http://www.huihui.cn/share/28896276http://www.huihui.cn/share/28897528http://www.huihui.cn/share/28899132http://www.huihui.cn/share/28900130http://blog.e-works.net.cn/681841/articles/849279.htmlhttp://blog.e-works.net.cn/681841/articles/849074.htmlhttp://blog.e-works.net.cn/681841/articles/848966.htmlhttp://blog.e-works.net.cn/681841/articles/848872.htmlhttp://blog.e-works.net.cn/681841/articles/848856.htmlhttp://blog.e-works.net.cn/681841/articles/848718.htmlhttp://blog.e-works.net.cn/681841/articles/848695.htmlExpress简单的结构图:
Express的特性:
- 基于Connect构建
- 健壮的路由
- 提供丰富的HTTP处理方法
- 支持众多视图模板引擎(14+)
- 内容协商
- 专注于提供高性能
- 环境基于配置
- 快速构建可执行的应用程序
- 高测试覆盖率
前端框架简介
Bootstrap
Bootstrap是Twitter推出的一个用于前端开发的开源工具包。它由Twitter的设计师MarkOtto和JacobThornton合作开发,是一个CSS/HTML框架。Bootstrap是简洁、直观、强悍的前端开发框架,让web开发更迅速、简单。
同时,很多基于Bootstrap的开源插件也让Bootstrap社区更加活跃。
最新的Bootstrap3提供了非常强的定制化特性。包括Less,jQuery插件等。
Bootstrap 为您提供了所有这些基本的模块- Grid、Typography、Tables、Forms、Buttons和Responsiveness。
此外,还有大量其他有用的前端组件,比如Dropdowns、Navigation、Modals、Typehead、Pagination、Carousal、Breadcrumb、Tab、Thumbnails、Headers等等。
有了这些,你可以搭建一个Web 项目,并让它运行地更快速更轻松。
此外,由于整个框架是基于模块的,你可以通过定制你自己的CSS来使得它满足你的特殊需求。
它是基于几种最佳实践,我们认为这是一个很好的开始学习现代Web 开发的时机,一旦你掌握了HTML 和JavaScript/jQuery 的基本知识,你就可以在Web 开发中运用这些知识。
Ember.js
Ember.js是一个JavaScript的MVC框架,它由Apple前雇员创建的SproutCore2.0改名进化而来。
构建一个Ember应用程序,通常会使用到六个主要部件:应用程序(Application)、模型(Model)、视图(View)、模板(Template)、路由(Routing)和控制器(Controller)。
这里我们server端主要依赖express框架,它提供的这些功能跟express有些是相同的。我们主要应用了Ember的模板组件,Express对于它提供了很好的集成。我们只需要进行很简单的配置即可:
app.set('view engine', 'html'); app.set('views', path.join(__dirname, 'views'));app.set("view options", {layout : false});app.register('.html', require('ejs'));
测试框架简介
should.js
should 是用于node.js的一个表述性、可读性很强的测试无关的“断言”库。它是BDD风格的,用一个单例的不可枚举的属性访问器扩展了Object的prototype,允许你表述对象应该展示的行为。
should的一个特性是可以支持链式断言,比如:
user.should.be.an.instanceOf(Object).and.have.property('name', 'tj');user.pets.should.be.instanceof(Array).and.have.lengthOf(4);
mocha
Mocha是一个简单、灵活、有趣的javascript测试框架。它可以同时适用于node.js跟浏览器端。可以很轻松得完成同步、异步测试,对产生异常的测试用例提供灵活以及准确的报告。可以通过接口实现BDD、TDD、QUnit风格。
mocha + should.js
mocha是可以实现BDD模式的测试框架,而should.js是BDD模式的断言库。把它们结合起来,可以组建出一个非常灵活,并且不失表述性的测试框架。我们所有给终端的接口,都基于它们完成了测试。
项目组件 - node modules
mysql
功能简介:mysql- node.js平台mysql驱动,支持事务、连接池、集群、sql注入检测、多做参数传递写法等特性。
主页地址:https://github.com/felixge/node-mysql
eventproxy
功能简介:eventproxy- node.js 异步回调代理。主要用来解决node中深层次回调嵌套的问题,支持很多异步模式:多类型异步、重复异步、持续型异步。
主页地址:https://github.com/JacksonTian/eventproxy
validator
功能简介:javascript的验证工具集,支持两种模式:check(校验)/sanitize(处理),同时提供了可扩展的错误处理。
主页地址:http://github.com/chriso/node-validator
ejs
功能简介:embered.jsjavascript 模板引擎(可以跟express集成,作为服务端模板引擎)
主页地址:https://github.com/visionmedia/ejs
loader
功能简介:loader- 资源加载工具,可以区分开发模式、发布模式;在发布模式下可进行资源压缩、合并。以实现减少静态资源带宽并且便于实现客户端缓存
主页地址:https://github.com/TBEDP/loader
canvas
功能简介:canvas - node.js 常用的图形图像处理库,是很多其它库的基础依赖库
主页地址:https://github.com/learnboost/node-canvas
captchagen
功能简介:captchagen-node.js常用验证码图片处理库,依赖上面的canvas库
主页地址:http://github.com/wearefractal/captchagen
crypto-js
功能简介:crypto-js- javascript 常用加密库、hash库封装,支持sha-x / md5 / hash等各种加密、hash算法
主页地址:http://github.com/wearefractal/captchagen
nodemailer
功能简介:nodemailer- 邮件发送工具,支持SMTP等邮件发送协议
主页地址:http://github.com/andris9/nodemailer
qrcode
功能简介:qrcode- node.js服务端的qrcode生成器。支持多种输出类型(dataUrl/file/bitArray)
主页地址:http://github.com/soldair/node-qrcode
pdfkit
功能简介:qrcode- node.js服务端的qrcode生成器。支持多种输出类型(dataUrl/file/bitArray)
主页地址:http://github.com/soldair/node-qrcode
excel
功能简介:excel- node.js excel解析器,支持xlsx(Excel2007+)
主页地址:https://github.com/trevordixon/excel
excel-export
功能简介:excel-export- node.js excel生成器,支持导出excel
主页地址:https://github.com/functionscope/Node-Excel-Export
net-ping
功能简介:net-ping- node.js 对ping的封装,用于测试目标主机是否可达
主页地址:https://bitbucket.org/stephenwvickers/node-net-ping
debug
功能简介:debug- node.js debug工具,对console.log的封装,支持多种颜色输出。
主页地址:https://github.com/visionmedia/debug
项目组织结构
NPM - Node.js 模块依赖管理工具
npm是管理node.js模块依赖的工具,依赖于开源技术的优势就是你有非常多的优秀库可以帮助你快速构建一个系统,但就像一把双刃剑,由于开源导致版本的升级不可控。这时,一个集中性的模块依赖管理工具的优势就十分明显。它负责帮你管理开源项目的版本,你只需要添加对某个开源模块的依赖即可。
unix/linux下安装npm:
curl http://npmjs.org/install.sh | sudo sh
如何在项目中使用npm管理你的依赖:
(1)在项目的根目录下创建一个package.json文件
在dependencies下添加所需要依赖的模块,示例如下:
打开terminal,进入项目的根目录下:
$ cd projectPath
安装这些module到本地repository:
$ npm install
这时你会发现,项目的根目录下多了一个node_modules文件夹,那里面就是从npm远程库里下载的模块然后“安装”到你的项目中的。
现在,你就可以在你的项目中应用你依赖的这些modules了。你可以通过require关键字来使用他们。比如,
require("eventproxy");
Node.js 模块加载机制
node.js的模块加载基于CommonJS规范。
在Node.js中,将模块分为两大类:
(1)原生模块
原生模块在Node.js源代码编译的时候编译进了二进制执行文件,加载速度最快。
(2)文件模块
node.js依赖modulepath(模块路径)来加载module,而modulepath的生成规则主要是从当前文件目录开始查找node_modules文件夹,然后依次进入父目录查找父目录下的node_modules目录直至到根目录下得node_modules目录。所以在require的时候,如果带上module的路径,则按照该路径查找,如果没有就按照上面的node_modules文件夹向上追溯查找,如果都没有找到,则抛出异常。
自动化部署
项目环境的构建、部署都是自动化的。
我们假设项目最终会发布在任意版本的Ubuntuserver上。在安装git的前提下,通过如下命令去clone项目到本地:
git clone git://github.com/yanghua/FixedAssetManager_Server.git
项目doc下有四个shell文件:
- node_install_ubuntu.sh - 在ubuntuserver上安装node.js的脚本
- node-canvas-install_ubuntu.sh - 在Ubuntuserver上安装node-canvas的脚本
- mysql_install_ubuntu.sh - 在Ubuntu server上安装mysql的脚本
- dispatch.sh - 部署项目的脚本
将它们copy到当前用户的home目录下,依次执行即可。整个过程几乎实现了无需人为干涉的“自动化”。
pm2 - 线上监控管理工具
pm2是非常优秀工具,它提供对基于node.js的项目运行托管服务。它基于命令行界面,提供很多特性:
- 内置的负载均衡器(使用nodecluster module)
- 以守护进程运行
- 0s(不间断)重启
- 为ubuntu/ CentOS 提供启动脚本
- 关闭不稳定的进程(避免无限死循环)
- 基于控制台监控
- HTTP API
- 远程控制以及实时监控接口
pm2使用nodecluster构建一个内置的负载均衡器。部署多个app的实例来达到分流的目的以减轻单app处理的压力。
异常监控与邮件推送
node.js 到处都是异步调用。常用的try/catch同步捕获异常并处理的方式,在这里不起作用了。这是因为很多callback已经离开了当时try的上下文,导致无法获取异常产生的堆栈信息。基于这个问题,我们对异常处理的模式按类型进行区分处理:
(1)http请求异常
这种异常Express就可以进行处理。如果是非法请求,在路由的时候,对未匹配的请求进行统一处理:
app.get("*", others.fourofour);
(2)业务异常
这种异常通常不会影响到程序的运行,我们以不同的异常代码返回给前端或者终端,来给调用端友好的提示。
(3)应用程序级别的异常或必须处理的错误
这种情况下,应用程序可能没有办法处理异常,也有可能由应用程序抛出。对于这种应用程序级别的异常。我们用两种方式来catch:
[1]利用Express提供的应用程序的异常处理机制:
app.error(function(err, req, res, next) { mailServie.sendMail({ subject : "FixedAssetManager_Server[App Error]", text : err.message + "\n" + err.stack + "\n" + err.toString() }); if (err instanceof PageNotFoundError) { res.render("errors/404"); } else if (err instanceof ServerError) { res.render("errors/500"); } });
[2]应用程序已经无法响应处理了,则利用node.js提供的,对于进程级别的异常处理方式:
process.on("uncaughtException", function (err) { mailServie.sendMail({ subject : "FixedAssetManager_Serv
0 0