﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-The NoteBook of EricKong-随笔分类-Node.js</title><link>http://www.blogjava.net/jjshcc/category/54617.html</link><description>桑巴葡语翻译工作室是广州市最专业葡萄牙语翻译机构-www.puyufanyi.com-欢迎寻找Java,As400,Mainframe的猎头eric_cc#qq.ccom(把#换成@)</description><language>zh-cn</language><lastBuildDate>Tue, 18 Nov 2014 11:13:49 GMT</lastBuildDate><pubDate>Tue, 18 Nov 2014 11:13:49 GMT</pubDate><ttl>60</ttl><item><title>Nodejs开发框架Express3.0开发手记</title><link>http://www.blogjava.net/jjshcc/archive/2014/11/14/420095.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Fri, 14 Nov 2014 08:18:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2014/11/14/420095.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/420095.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2014/11/14/420095.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/420095.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/420095.html</trackback:ping><description><![CDATA[<h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">Nodejs给Javascript赋予了服务端应用的生命，Jquery让Javascript成为浏览中开发的利器。 最近学习了Nodejs的Express3.0的开发框架，本来是按照&#8220;node.js开发指南&#8221;书中介绍，但&#8220;node.js开发指南&#8221;讲的是Express2.x的，从Express2.x到Express3.0自己模索中还是走了不少弯路的。写篇文章总结一下。</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">关于作者</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">张丹(Conan), 程序员Java,R,PHP,Javacript<br />weibo：@Conan_Z<br />blog: http://blog.fens.me<br />email: bsspirit@gmail.com</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>转载请注明出处：</strong><br /><a title="Nodejs开发框架Express3.0开发手记--从零开始" href="http://blog.fens.me/nodejs-express3/" target="_blank" style="color: #0e73b8;">http://blog.fens.me/nodejs-express3/</a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>程序代码已经上传到github有需要的同学，自行下载。</strong><br /><a title="github工程" href="https://github.com/bsspirit/nodejs-demo" target="_blank" style="color: #0e73b8;">https://github.com/bsspirit/nodejs-demo</a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a title="Nodejs开发框架Express3.0开发手记--从零开始" href="http://blog.fens.me/wp-content/uploads/2013/05/nodejs-1.png" style="color: #0e73b8;"><img class="alignnone size-full wp-image-171" alt="nodejs intro" src="http://blog.fens.me/wp-content/uploads/2013/05/nodejs-1.png" width="600" height="400" /></a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>从零开始nodejs系列文章</strong></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a title="从零开始nodejs系列文章" href="http://blog.fens.me/series-nodejs/" target="_blank" style="color: #0e73b8;">从零开始nodejs系列文章</a>，将介绍如何利Javascript做为服务端脚本，通过Nodejs框架web开发。Nodejs框架是基于V8的引擎，是目前速度最快的Javascript引擎。chrome浏览器就基于V8，同时打开20-30个网页都很流畅。Nodejs标准的web开发框架Express，可以帮助我们迅速建立web站点，比起PHP的开发效率更高，而且学习曲线更低。非常适合小型网站，个性化网站，我们自己的Geek网站！！</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">目录</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">此文重点介绍Express3.0的开发框架，其中还会涉及到Mongoose,Ejs,Bootstrap等相关内容。</p><ol style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li>建立工程</li><li>目录结构</li><li>Express3.0配置文件</li><li>Ejs模板使用</li><li>Bootstrap界面框架</li><li>路由功能</li><li>Session使用</li><li>页面提示</li><li>页面访问控制</li></ol></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">开发环境：</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>Win7旗舰版 64bit</strong></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>MonogoDB: v2.4.3</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
Tue May 14 09:24:50.118 [initandlisten] MongoDB starting : pid=1716 port=27017 dbpath=./data 64-bit host=PC201304202140
Tue May 14 09:24:50.119 [initandlisten] db version v2.4.3
Tue May 14 09:24:50.119 [initandlisten] git version: fe1743177a5ea03e91e0052fb5e2cb2945f6d95f
Tue May 14 09:24:50.119 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
Tue May 14 09:24:50.119 [initandlisten] allocator: system
Tue May 14 09:24:50.119 [initandlisten] options: { dbpath: "./data" }
Tue May 14 09:24:50.188 [initandlisten] journal dir=./data\journal
Tue May 14 09:24:50.189 [initandlisten] recover : no journal files present, no recovery needed
Tue May 14 09:24:50.441 [initandlisten] preallocateIsFaster=true 3.26
Tue May 14 09:24:50.778 [initandlisten] preallocateIsFaster=true 5.88
Tue May 14 09:24:51.827 [initandlisten] waiting for connections on port 27017
Tue May 14 09:24:51.827 [websvr] admin web console waiting for connections on port 28017
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>nodejs: v0.10.5, npm 1.2.19</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>node -v
v0.10.5
npm -v
1.2.19
</code></pre></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">1. 建立工程</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>进入工程目录</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
cd D:\workspace\project
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>全局安装express，express作为命令被安装到了系统中</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
npm install -g express
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>查看express版本</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
express -V
3.2.2
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>使用express命令创建工程，并支持ejs</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
D:\workspace\project&gt;express -e nodejs-demo

create : nodejs-demo
create : nodejs-demo/package.json
create : nodejs-demo/app.js
create : nodejs-demo/public
create : nodejs-demo/public/javascripts
create : nodejs-demo/public/images
create : nodejs-demo/public/stylesheets
create : nodejs-demo/public/stylesheets/style.css
create : nodejs-demo/routes
create : nodejs-demo/routes/index.js
create : nodejs-demo/routes/user.js
create : nodejs-demo/views
create : nodejs-demo/views/index.ejs

install dependencies:
$ cd nodejs-demo &amp;&amp; npm install
run the app:
$ node app
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>根据提示，下载依赖包</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
cd nodejs-demo &amp;&amp; npm install

express@3.2.2 node_modules\express
&#9500;&#9472;&#9472; methods@0.0.1
&#9500;&#9472;&#9472; fresh@0.1.0
&#9500;&#9472;&#9472; buffer-crc32@0.2.1
&#9500;&#9472;&#9472; range-parser@0.0.4
&#9500;&#9472;&#9472; cookie-signature@1.0.1
&#9500;&#9472;&#9472; cookie@0.0.5
&#9500;&#9472;&#9472; qs@0.6.3
&#9500;&#9472;&#9472; commander@0.6.1
&#9500;&#9472;&#9472; debug@0.7.2
&#9500;&#9472;&#9472; mkdirp@0.3.4
&#9500;&#9472;&#9472; send@0.1.0 (mime@1.2.6)
&#9492;&#9472;&#9472; connect@2.7.8 (pause@0.0.1, bytes@0.2.0, formidable@1.0.13)
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>模板项目建立成功，启动模板项目。</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
D:\workspace\project\nodejs-demo&gt;node app.js
Express server listening on port 3000
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">本地的3000端口被打开，通过浏览器访问: localhost:3000</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">通过node启动程序，每次代码修改都需要重新启动。 有一个工具supervisor，每次修改代码后会自动重启，会我们开发省很多的时间。</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
npm install supervisor
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>再启动服务</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
D:\workspace\project\nodejs-demo&gt;supervisor app.js

DEBUG: Running node-supervisor with
DEBUG: program 'app.js'
DEBUG: --watch '.'
DEBUG: --ignore 'undefined'
DEBUG: --extensions 'node|js'
DEBUG: --exec 'node'

DEBUG: Starting child process with 'node app.js'
DEBUG: Watching directory 'D:\workspace\project\nodejs-demo' for changes.
Express server listening on port 3000
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">2. 目录结构</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">D:\workspace\project\nodejs-demo&gt;dir</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">2013/05/14 09:42 877 app.js<br />2013/05/14 09:48 &lt;DIR&gt; node_modules<br />2013/05/14 09:42 184 package.json<br />2013/05/14 09:42 &lt;DIR&gt; public<br />2013/05/14 09:42 &lt;DIR&gt; routes<br />2013/05/14 09:42 &lt;DIR&gt; views</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>目录介绍：</strong></p><ul style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li><span style="font-size: 13px; line-height: 19px;">node_modules, 存放所有的项目依赖库。(每个项目管理自己的依赖，与Maven,Gradle等不同)</span></li><li><span style="font-size: 13px; line-height: 19px;">package.json，项目依赖配置及开发者信息</span></li><li><span style="font-size: 13px; line-height: 19px;">app.js，程序启动文件</span></li><li><span style="font-size: 13px; line-height: 19px;">public，静态文件(css,js,img)</span></li><li><span style="font-size: 13px; line-height: 19px;">routes，路由文件(MVC中的C,controller)</span></li><li><span style="font-size: 13px; line-height: 19px;">Views，页面文件(Ejs模板)</span></li></ul></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">3. Express3.0配置文件</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>打开app.js文件</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
/**
* 模块依赖
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();

//环境变量
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// 开发模式
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

// 路径解析
app.get('/', routes.index);
app.get('/users', user.list);

// 启动及端口
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">4. Ejs模板使用</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">让ejs模板文件，使用扩展名为html的文件。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>修改：app.js</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
app.engine('.html', ejs.__express);
app.set('view engine', 'html');// app.set('view engine', 'ejs');
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>修改后，ejs变量没有定义，supervisor的程序会一直报错</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
ReferenceError: ejs is not defined
at Object. (D:\workspace\project\nodejs-demo\app.js:17:21)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
DEBUG: Program node app.js exited with code 8
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>在app.js中增加ejs变量</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, ejs = require('ejs');
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>访问localhost:3000，程序报错</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
Error: Failed to lookup view "index"
at Function.app.render (D:\workspace\project\nodejs-demo\node_modules\express\lib\application.js:495:17)
at ServerResponse.res.render (D:\workspace\project\nodejs-demo\node_modules\express\lib\response.js:756:7)
at exports.index (D:\workspace\project\nodejs-demo\routes\index.js:7:7)
at callbacks (D:\workspace\project\nodejs-demo\node_modules\express\lib\router\index.js:161:37)
at param (D:\workspace\project\nodejs-demo\node_modules\express\lib\router\index.js:135:11)
at pass (D:\workspace\project\nodejs-demo\node_modules\express\lib\router\index.js:142:5)
at Router._dispatch (D:\workspace\project\nodejs-demo\node_modules\express\lib\router\index.js:170:5)
at Object.router (D:\workspace\project\nodejs-demo\node_modules\express\lib\router\index.js:33:10)
at next (D:\workspace\project\nodejs-demo\node_modules\express\node_modules\connect\lib\proto.js:190:15)
at Object.methodOverride [as handle] (D:\workspace\project\nodejs-demo\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5)
GET / 500 26ms
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>重命名：views/indes.ejs 为 views/index.html</strong></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">访问localhost:3000正确</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">5. 增加Bootstrap界面框架</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">其实就是把js,css文件复制到项目中对应该的目录里。 包括4个文件：</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">复制到public/stylesheets目录</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
bootstrap.min.css
bootstrap-responsive.min.css
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">复制到public/javascripts目录</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
bootstrap.min.js
jquery-1.9.1.min.js
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">接下来，我们把index.html页面切分成3个部分：header.html, index.html, footer.html</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">header.html, 为html页面的头部区域<br />index.html, 为内容显示区域<br />footer.html，为页面底部区域</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>header.html</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;&lt;%=: title %&gt;&lt;/title&gt;
&lt;!-- Bootstrap --&gt;
&lt;link href="/stylesheets/bootstrap.min.css" rel="stylesheet" media="screen"&gt;
&lt;!-- &lt;link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen"&gt; --&gt;
&lt;/head&gt;
&lt;body screen_capture_injected="true"&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>index.html</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;% include header.html %&gt;
&lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
&lt;p&gt;Welcome to &lt;%= title %&gt;&lt;/p&gt;
&lt;% include footer.html %&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">注：express3.0时，ejs嵌入其他页面时使用include，express2.x用法不一样。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>footer.html</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;script src="/javascripts/jquery-1.9.1.min.js"&gt;&lt;/script&gt;
&lt;script src="/javascripts/bootstrap.min.js"&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">访问localhost:3000正确。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">我们已经成功的使用了EJS模板的功能，把公共的头部和底部从页面中分离出来了。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">并已经引入了bootstrap界面框架，后面讲到&#8220;登陆界面&#8221;的时候，就会看到bootstrap界面效果了。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">6. 路由功能</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">我们设计一下用户登陆业务需求。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">访问路径：/，页面：index.html，不需要登陆，可以直接访问。<br />访问路径：/home，页面：home.html，必须用户登陆后，才可以访问。<br />访问路径：/login，页面：login.html，登陆页面，用户名密码输入正确，自动跳转到home.html<br />访问路径：/logout，页面：无，退出登陆后，自动回到index.html页面<br />打开app.js文件，在增加路由配置</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
app.get('/', routes.index);
app.get('/login', routes.login);
app.post('/login', routes.doLogin);
app.get('/logout', routes.logout);
app.get('/home', routes.home);
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">注：get为get请求，post为post请求，all为所有针对这个路径的请求</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">我们打开routes/index.js文件，增加对应的方法。</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
exports.index = function(req, res){
res.render('index', { title: 'Index' });
};
exports.login = function(req, res){
res.render('login', { title: '用户登陆'});
};
exports.doLogin = function(req, res){
var user={
username:'admin',
password:'admin'
}
if(req.body.username===user.username &amp;&amp; req.body.password===user.password){
res.redirect('/home');
}
res.redirect('/login');
};
exports.logout = function(req, res){
res.redirect('/');
};
exports.home = function(req, res){
var user={
username:'admin',
password:'admin'
}
res.render('home', { title: 'Home',user: user});
};
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">创建views/login.html和views/home.html两个文件</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>login.html</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;% include header.html %&gt;
&lt;div class="container-fluid"&gt;
&lt;form class="form-horizontal" method="post"&gt;
&lt;fieldset&gt;
&lt;legend&gt;用户登陆&lt;/legend&gt;
&lt;div class="control-group"&gt;
&lt;label class="control-label" for="username"&gt;用户名&lt;/label&gt;
&lt;div class="controls"&gt;
&lt;input type="text" class="input-xlarge" id="username" name="username"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="control-group"&gt;
&lt;label class="control-label" for="password"&gt;密码&lt;/label&gt;
&lt;div class="controls"&gt;
&lt;input type="password" class="input-xlarge" id="password" name="password"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="form-actions"&gt;
&lt;button type="submit" class="btn btn-primary"&gt;登陆&lt;/button&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;% include footer.html %&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a href="http://blog.fens.me/wp-content/uploads/2013/05/login.png" style="color: #0e73b8;"><img class="alignnone size-full wp-image-167" alt="login" src="http://blog.fens.me/wp-content/uploads/2013/05/login.png" width="771" height="260" /></a><br /><strong>注：使用了bootstrap界面框架，效果还不错吧.</strong></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>home.html</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;% include header.html %&gt;
&lt;h1&gt;Welcome &lt;%= user.username %&gt;, 欢迎登陆！！&lt;/h1&gt;
&lt;a claa="btn" href="/logout"&gt;退出&lt;/a&gt;
&lt;% include footer.html %&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">修改index.html，增加登陆链接<br /><strong>index.html</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;% include header.html %&gt;
&lt;h1&gt;Welcome to &lt;%= title %&gt;&lt;/h1&gt;
&lt;p&gt;&lt;a href="/login"&gt;登陆&lt;/a&gt;&lt;/p&gt;
&lt;% include footer.html %&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">路由及页面我们都写好了，快去网站上试试吧。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">7. Session使用</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">从刚来的例子上面看，执行exports.doLogin时，如果用户名和密码正确，我们使用redirect方法跳转到的home</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>res.redirect('/home');</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">执行exports.home时，我们又用render渲染页面，并把user对象传给home.html页面</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>res.render('home', { title: 'Home',user: user});</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">为什么不能在doLogin时，就把user对象赋值给session，每个页面就不再传值了。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">session这个问题，其实是涉及到服务器的底层处理方式。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">像Java的web服务器，是多线程调用模型。每用户请求会打开一个线程，每个线程在内容中维护着用户的状态。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">像PHP的web服务器，是交行CGI的程序处理，CGI是无状态的，所以一般用cookie在客户的浏览器是维护用户的状态。但cookie在客户端维护的信息是不够的，所以CGI应用要模仿用户session，就需要在服务器端生成一个session文件存储起来，让原本无状态的CGI应用，通过中间文件的方式，达到session的效果。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">Nodejs的web服务器，也是CGI的程序无状态的，与PHP不同的地方在于，单线程应用，所有请求都是异步响应，通过callback方式返回数据。如果我们想保存session数据，也是需要找到一个存储，通过文件存储,redis,Mongdb都可以。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">接下来，我将演示如何通过mongodb来保存session，并实现登陆后用户对象传递。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">app.js文件</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, ejs = require('ejs')
, SessionStore = require("session-mongoose")(express);
var store = new SessionStore({
url: "mongodb://localhost/session",
interval: 120000
});
....
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.cookieSession({secret : 'fens.me'}));
app.use(express.session({
secret : 'fens.me',
store: store,
cookie: { maxAge: 900000 }
}));
app.use(function(req, res, next){
res.locals.user = req.session.user;
next();
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>注：app.js文件有顺序要求，一定要注意！！！</strong></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">安装session-mongoose依赖库</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
D:\workspace\project\nodejs-demo&gt;npm install session-mongoose
D:\workspace\project\nodejs-demo\node_modules\session-mongoose\node_modules\mongoose\node_modules\mongodb\node_modules\bson&gt;node "D:\toolkit\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.Cpp.InvalidPlatform.Targets(23,7): error MSB8007: 项目&#8220;kerberos.vcxproj&#8221;的平台无效。平台为&#8220;x64&#8221;。您会看到此消息的可能原因是，您尝试在没有解决方案文件的情况下生成项目，并且为
oose\node_modules\mongoose\node_modules\mongodb\node_modules\bson\build\bson.vcxproj]
session-mongoose@0.2.2 node_modules\session-mongoose
&#9492;&#9472;&#9472; mongoose@3.6.10 (mpath@0.1.1, ms@0.1.0, hooks@0.2.1, sliced@0.0.3, muri@0.3.1, mpromise@0.2.1, mongodb@1.3.3)
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">安装有错误但是没关系。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">访问：http://localhost:3000/login，正常</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">修改routes/index.js文件</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>exports.doLogin方法</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
exports.doLogin = function(req, res){
var user={
username:'admin',
password:'admin'
}
if(req.body.username===user.username &amp;&amp; req.body.password===user.password){
req.session.user=user;
return res.redirect('/home');
} else {
return res.redirect('/login');
}
};
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>exports.logout方法</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
exports.logout = function(req, res){
req.session.user=null;
res.redirect('/');
};
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>exports.home方法</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
exports.home = function(req, res){
res.render('home', { title: 'Home'});
};
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">这个时候session已经起作用了，exports.home的user显示传值已经被去掉了。 是通过app.js中app.use的res.locals变量，通过框架进行的赋值。</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
app.use(function(req, res, next){
res.locals.user = req.session.user;
next();
});
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">注：这个session是express3.0的写法，与express2.x是不一样的。原理是在框架内每次赋值，把我们刚才手动传值的过程，让框架去完成了。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">8. 页面提示</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">登陆的大体我们都已经讲完了，最后看一下登陆失败的情况。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">我们希望如果用户登陆时，用户名或者密码出错了，会给用户提示，应该如何去实现。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>打开app.js的，增加res.locals.message</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
app.use(function(req, res, next){
res.locals.user = req.session.user;
var err = req.session.error;
delete req.session.error;
res.locals.message = '';
if (err) res.locals.message = '&lt;div class="alert alert-error"&gt;' + err + '&lt;/div&gt;';
next();
});
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>修改login.html页面，&lt;%- message %&gt;</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
&lt;% include header.html %&gt;
&lt;div class="container-fluid"&gt;
&lt;form class="form-horizontal" method="post"&gt;
&lt;fieldset&gt;
&lt;legend&gt;用户登陆&lt;/legend&gt;
&lt;%- message %&gt;
&lt;div class="control-group"&gt;
&lt;label class="control-label" for="username"&gt;用户名&lt;/label&gt;
&lt;div class="controls"&gt;
&lt;input type="text" class="input-xlarge" id="username" name="username" value="admin"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="control-group"&gt;
&lt;label class="control-label" for="password"&gt;密码&lt;/label&gt;
&lt;div class="controls"&gt;
&lt;input type="password" class="input-xlarge" id="password" name="password" value="admin"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="form-actions"&gt;
&lt;button type="submit" class="btn btn-primary"&gt;登陆&lt;/button&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;% include footer.html %&gt;
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>修改routes/index.js，增加req.session.error</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
exports.doLogin = function(req, res){
var user={
username:'admin',
password:'admin'
}
if(req.body.username===user.username &amp;&amp; req.body.password===user.password){
req.session.user=user;
return res.redirect('/home');
} else {
req.session.error='用户名或密码不正确';
return res.redirect('/login');
}
};
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">让我们来看看效果： http://localhost:3000/login 输入错误的和密码， 用户名：adminfe，密码：12121</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a href="http://blog.fens.me/wp-content/uploads/2013/05/loginErr.png" style="color: #0e73b8;"><img class="alignnone size-full wp-image-168" alt="loginErr" src="http://blog.fens.me/wp-content/uploads/2013/05/loginErr.png" width="760" height="290" /></a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">&nbsp;</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">9. 页面访问控制</h2><h2><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">网站登陆部分按照我们的求已经完成了，但网站并不安全。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">localhost:3000/home，页面本来是登陆以后才访问的，现在我们不要登陆，直接在浏览器输入也可访问。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>页面报错了，提示&lt;%= user.username %&gt; 变量出错。</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> GET /home?user==a 500 15ms TypeError: D:\workspace\project\nodejs-demo\views\home.html:2 1| &lt;% include header.html %&gt; &gt;&gt; 2| &lt;h1&gt;Welcome &lt;%= user.username %&gt;, 欢迎登陆！！&lt;/h1&gt; 3| &lt;a claa="btn" href="/logout"&gt;退出&lt;/a&gt; 4| &lt;% include header.html %&gt; Cannot read property 'username' of null at eval (eval at &lt;anonymous&gt; (D:\workspace\project\nodejs-demo\node_modules\ejs\lib\ejs.js: at eval (eval at &lt;anonymous&gt; (D:\workspace\project\nodejs-demo\node_modules\ejs\lib\ejs.js: at D:\workspace\project\nodejs-demo\node_modules\ejs\lib\ejs.js:249:15 at Object.exports.render (D:\workspace\project\nodejs-demo\node_modules\ejs\lib\ejs.js:287: at View.exports.renderFile [as engine] (D:\workspace\project\nodejs-demo\node_modules\ejs\l at View.render (D:\workspace\project\nodejs-demo\node_modules\express\lib\view.js:75:8) at Function.app.render (D:\workspace\project\nodejs-demo\node_modules\express\lib\applicati at ServerResponse.res.render (D:\workspace\project\nodejs-demo\node_modules\express\lib\res at exports.home (D:\workspace\project\nodejs-demo\routes\index.js:36:8) at callbacks (D:\workspace\project\nodejs-demo\node_modules\express\lib\router\index.js:161 </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">这个页面被打开发，因为没有user.username参数。我们避免这样的错误发生。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">还记录路由部分里说的get,post,all的作用吗？我现在要回到路由配置中，再做点事情。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>修改app.js文件</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> app.all('/login', notAuthentication); app.get('/login', routes.login); app.post('/login', routes.doLogin); app.get('/logout', authentication); app.get('/logout', routes.logout); app.get('/home', authentication); app.get('/home', routes.home); </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>访问控制：</strong></p><ul style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li><span style="font-size: 13px; line-height: 19px;">/ ，谁访问都行，没有任何控制</span></li><li><span style="font-size: 13px; line-height: 19px;">/login，用all拦截所有访问/login的请求，先调用authentication，用户登陆检查</span></li><li><span style="font-size: 13px; line-height: 19px;">/logout，用get拦截访问/login的请求，先调用notAuthentication，用户不登陆检查</span></li><li><span style="font-size: 13px; line-height: 19px;">/home，用get拦截访问/home的请求，先调用Authentication，用户登陆检查</span></li></ul><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>修改app.js文件，增加authentication，notAuthentication两个方法</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> function authentication(req, res, next) { if (!req.session.user) { req.session.error='请先登陆'; return res.redirect('/login'); } next(); } function notAuthentication(req, res, next) { if (req.session.user) { req.session.error='已登陆'; return res.redirect('/'); } next(); } </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">配置好后，我们未登陆，直接访问localhost:3000/home时，就会跳到/login页面</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a href="http://blog.fens.me/wp-content/uploads/2013/05/loginHome.png" style="color: #ff0505;"><img size-full=""  wp-image-169"="" alt="loginHome" src="http://blog.fens.me/wp-content/uploads/2013/05/loginHome.png" width="768" height="295" /></a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">如果你也出现图片显示的内容，那么恭喜你了。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">Nodejs使用Express3.0框架的第一步你已经完成了，并且还使用了ejs,bootstrap,mongoose库的使用。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">希望此文对大家有所帮助。</p></h2><img src ="http://www.blogjava.net/jjshcc/aggbug/420095.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2014-11-14 16:18 <a href="http://www.blogjava.net/jjshcc/archive/2014/11/14/420095.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>bower解决js的依赖管理</title><link>http://www.blogjava.net/jjshcc/archive/2014/11/14/420079.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Fri, 14 Nov 2014 05:50:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2014/11/14/420079.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/420079.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2014/11/14/420079.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/420079.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/420079.html</trackback:ping><description><![CDATA[<h2><div style="display: inline-block;"></div></h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a title="从零开始nodejs系列文章" href="http://blog.fens.me/series-nodejs/" target="_blank" style="color: #0e73b8;">从零开始nodejs系列文章</a>，将介绍如何利Javascript做为服务端脚本，通过Nodejs框架web开发。Nodejs框架是基于V8的引擎，是目前速度最快的Javascript引擎。chrome浏览器就基于V8，同时打开20-30个网页都很流畅。Nodejs标准的web开发框架Express，可以帮助我们迅速建立web站点，比起PHP的开发效率更高，而且学习曲线更低。非常适合小型网站，个性化网站，我们自己的Geek网站！！</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>关于作者</strong></p><ul style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li>张丹(Conan), 程序员Java,R,PHP,Javascript</li><li>weibo：@Conan_Z</li><li>blog:&nbsp;<a title="粉丝日志|跨界的IT博客" href="http://blog.fens.me/" target="_blank" style="color: #0e73b8;">http://blog.fens.me</a></li><li>email: bsspirit@gmail.com</li></ul><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>转载请注明出处：</strong><br /><a title="bower解决js的依赖管理" href="http://blog.fens.me/nodejs-bower-intro/" target="_blank" style="color: #0e73b8;">http://blog.fens.me/nodejs-bower-intro/</a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><a href="http://blog.fens.me/wp-content/uploads/2013/08/bower.png" style="color: #0e73b8;"><img src="http://blog.fens.me/wp-content/uploads/2013/08/bower.png" alt="bower" width="600" height="400" class="alignnone size-full wp-image-1821" /></a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>前言</strong><br />一个新的web项目开始，我们总是很自然地去下载需要用到的js类库文件，比如jQuery，去官网下载名为jquery-1.10.2.min.js文件，放到我们的项目里。当项目又需要bootstrap的时候，我们会重复刚才的工作，去bootstrap官网下载对应的类库。如果bootstrap所依赖的jQuery并不是1.10.2，而是2.0.3时，我们会再重新下载一个对应版本的jQuery替换原来的。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">包管理是个复杂的问题，我们要知道谁依赖谁，还要明确哪个版本依赖哪个版本。这些对于开发人员来说，负担过重了。bower作为一个js依赖管理的工具，提供一种理想包管理方式，借助了npm的一些思想，为我们提供一个舒服的开发环境。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">你要还不动起手来试试bower，那你一定不会知道，前端开发是件多么享受的事。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>目录</strong></p><ol style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li>bower介绍</li><li>bower安装</li><li>bower命令</li><li>bower使用</li><li>用bower提交自己类库</li></ol></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">1. bower介绍</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">Bower 是 twitter 推出的一款包管理工具，基于nodejs的模块化思想，把功能分散到各个模块中，让模块和模块之间存在联系，通过 Bower 来管理模块间的这种联系。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">包管理工具一般有以下的功能：</p><ul style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li>注册机制：每个包需要确定一个唯一的 ID 使得搜索和下载的时候能够正确匹配，所以包管理工具需要维护注册信息，可以依赖其他平台。</li><li>文件存储：确定文件存放的位置，下载的时候可以找到，当然这个地址在网络上是可访问的。</li><li>上传下载：这是工具的主要功能，能提高包使用的便利性。比如想用 jquery 只需要 install 一下就可以了，不用到处找下载。上传并不是必备的，根据文件存储的位置而定，但需要有一定的机制保障。</li><li>依赖分析：这也是包管理工具主要解决的问题之一，既然包之间是有联系的，那么下载的时候就需要处理他们之间的依赖。下载一个包的时候也需要下载依赖的包。</li></ul><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">功能介绍，摘自文章：<a href="http://chuo.me/2013/02/twitter-bower.html" target="_blank" rel="nofollow" style="color: #0e73b8;">http://chuo.me/2013/02/twitter-bower.html</a></p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">2. bower安装</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">bower插件是通过npm, Node.js包管理器安装和管理的.</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>我的系统环境</strong></p><ul style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li>win7 64bit</li><li>Nodejs:v0.10.5</li><li>Npm:1.2.19</li></ul><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>~ D:\workspace\javascript&gt;node -v
v0.10.5

~ D:\workspace\javascript&gt;npm -v
1.2.19
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">在系统中，我们已经安装好了Nodejs和npm。win7安装nodejs请参考文章：<a href="http://blog.fens.me/nodejs-express3/" target="_blank" style="color: #0e73b8;">Nodejs开发框架Express3.0开发手记&#8211;从零开始</a></p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>安装bower&nbsp;</strong><br />全局安装bower</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript&gt;npm install bower -g
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">新建一个express3的项目nodejs-bower</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript&gt;express -e nodejs-bower
~ D:\workspace\javascript&gt;cd nodejs-bower &amp;&amp; npm install
</code></pre></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">3. bower命令</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">bower安装后，我们就可以用bower这个命令了。</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>~ D:\workspace\javascript\nodejs-bower&gt;bower
Usage:
    bower <command></command> [<args>] [<options>]
Commands:
    cache                   Manage bower cache
    help                    Display help information about Bower
    home                    Opens a package homepage into your favorite browser
    info                    Info of a particular package
    init                    Interactively create a bower.json file
    install                 Install a package locally
    link                    Symlink a package folder
    list                    List local packages
    lookup                  Look up a package URL by name
    prune                   Removes local extraneous packages
    register                Register a package
    search                  Search for a package by name
    update                  Update a local package
    uninstall               Remove a local package
Options:
    -f, --force             Makes various commands more forceful
    -j, --json              Output consumable JSON
    -l, --log-level         What level of logs to report
    -o, --offline           Do not hit the network
    -q, --quiet             Only output important information
    -s, --silent            Do not output anything, besides errors
    -V, --verbose           Makes output more verbose
    --allow-root            Allows running commands as root
See 'bower help <command></command>' for more information on a specific command.
</options></args></code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">Commands，列出了bower支持的各种命令。</p><ul style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><li>cache:bower缓存管理</li><li>help:显示Bower命令的帮助信息</li><li>home:通过浏览器打开一个包的github发布页</li><li>info:查看包的信息</li><li>init:创建bower.json文件</li><li>install:安装包到项目</li><li>link:在本地bower库建立一个项目链接</li><li>list:列出项目已安装的包</li><li>lookup:根据包名查询包的URL</li><li>prune:删除项目无关的包</li><li>register:注册一个包</li><li>search:搜索包</li><li>update:更新项目的包</li><li>uninstall:删除项目的包</li></ul></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">4. bower使用</h2><h2><div style="display: inline-block;"><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>1). 安装jQuery到项目nodejs-bower</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower install jquery
bower jquery#*              not-cached git://github.com/components/jquery.git#*
bower jquery#*                 resolve git://github.com/components/jquery.git#*
bower jquery#*                download https://github.com/components/jquery/archive/2.0.3.tar.gz
bower jquery#*                 extract archive.tar.gz
bower jquery#*                resolved git://github.com/components/jquery.git#2.0.3
bower jquery#~2.0.3            install jquery#2.0.3

jquery#2.0.3 bower_components\jquery
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">通过执行命令，我们可以看到jQuery的最新版本被下载，并安装到项目的bower_components\jquery目录</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">查看bower_components/jquery目录，发现了3个文件。</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;ls bower_components/jquery -a
.  ..  .bower.json  component.json  jquery.js
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">同样地，我们的项目还需要d3的类库</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower install d3
bower d3#*                  not-cached git://github.com/mbostock/d3.git#*
bower d3#*                     resolve git://github.com/mbostock/d3.git#*
bower d3#*                    download https://github.com/mbostock/d3/archive/v3.2.8.tar.gz
bower d3#*                     extract archive.tar.gz
bower d3#*                    resolved git://github.com/mbostock/d3.git#3.2.8
bower d3#~3.2.8                install d3#3.2.8

d3#3.2.8 bower_components\d3
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">非常方便，下载并安装完成！</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>2). 查看项目中已导入的类库</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower list
bower check-new     Checking for new versions of the project dependencies..
nodejs-bower#0.0.0 D:\workspace\javascript\nodejs-bower
&#9500;&#9472;&#9472; d3#3.2.8
&#9492;&#9472;&#9472; jquery#2.0.3
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>3). 安装bootstrap库，并查看依赖情况</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower install bootstrap
bower bootstrap#*               cached git://github.com/twbs/bootstrap.git#3.0.0-rc1
bower bootstrap#*             validate 3.0.0-rc1 against git://github.com/twbs/bootstrap.git#*
bower jquery#&gt;= 1.9.0           cached git://github.com/components/jquery.git#2.0.3
bower jquery#&gt;= 1.9.0         validate 2.0.3 against git://github.com/components/jquery.git#&gt;= 1.9.0
bower bootstrap#~3.0.0-rc1     install bootstrap#3.0.0-rc1

bootstrap#3.0.0-rc1 bower_components\bootstrap
&#9492;&#9472;&#9472; jquery#2.0.3

~ D:\workspace\javascript\nodejs-bower&gt;bower list
bower check-new     Checking for new versions of the project dependencies..
nodejs-bower#0.0.0 D:\workspace\javascript\nodejs-bower
&#9500;&#9472;&#9516; bootstrap#3.0.0-rc1 extraneous
&#9474; &#9492;&#9472;&#9472; jquery#2.0.3
&#9500;&#9472;&#9472; d3#3.2.8
&#9492;&#9472;&#9472; jquery#2.0.3
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">我们发现bootstrap，对jquery是有依赖的。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>4). 删除jQuery库，破坏依赖关系</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower uninstall jquery
bower conflict      bootstrap depends on jquery
Continue anyway? (y/n) y
bower uninstall     jquery

~ D:\workspace\javascript\nodejs-bower&gt;bower list
bower check-new     Checking for new versions of the project dependencies..
nodejs-bower#0.0.0 D:\workspace\javascript\nodejs-bower
&#9500;&#9472;&#9516; bootstrap#3.0.0-rc1 extraneous
&#9474; &#9492;&#9472;&#9472; jquery missing
&#9500;&#9472;&#9472; d3#3.2.8
&#9492;&#9472;&#9472; jquery missing
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>5). 安装低版本的jQuery，制造不版本兼容</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower install jquery#1.7.2
bower jquery#~2.0.3             cached git://github.com/components/jquery.git#2.0.3
bower jquery#~2.0.3           validate 2.0.3 against git://github.com/components/jquery.git#~2.0.3
bower jquery#&gt;= 1.9.0           cached git://github.com/components/jquery.git#2.0.3
bower jquery#&gt;= 1.9.0         validate 2.0.3 against git://github.com/components/jquery.git#&gt;= 1.9.0
bower jquery#1.7.2              cached git://github.com/components/jquery.git#1.7.2
bower jquery#1.7.2            validate 1.7.2 against git://github.com/components/jquery.git#1.7.2

Unable to find a suitable version for jquery, please choose one:
    1) jquery#1.7.2 which resolved to 1.7.2
    2) jquery#~2.0.3 which resolved to 2.0.3 and has nodejs-bower as dependants
    3) jquery#&gt;= 1.9.0 which resolved to 2.0.3 and has bootstrap#3.0.0-rc1 as dependants

Prefix the choice with ! to persist it to bower.json

Choice: 1
bower jquery#1.7.2             install jquery#1.7.2

jquery#1.7.2 bower_components\jquery

~ D:\workspace\javascript\nodejs-bower&gt;bower list
bower check-new     Checking for new versions of the project dependencies..
nodejs-bower#0.0.0 D:\workspace\javascript\nodejs-bower
&#9500;&#9472;&#9516; bootstrap#3.0.0-rc1 extraneous
&#9474; &#9492;&#9472;&#9472; jquery#1.7.2 incompatible with &gt;= 1.9.0 (2.0.3 available)
&#9500;&#9472;&#9472; d3#3.2.8
&#9492;&#9472;&#9472; jquery#1.7.2 incompatible with ~2.0.3 (2.0.3 available)
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">我们可以清楚的看到bower，很明确的告诉了我们，jquery和bootstrap是不兼容的，强大之处大家应该有所体会。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>6).升级jQuery，让版本兼容</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower update jquery
bower jquery#~2.0.3             cached git://github.com/components/jquery.git#2.0.3
bower jquery#~2.0.3           validate 2.0.3 against git://github.com/components/jquery.git#~2.0.3
bower jquery#&gt;= 1.9.0           cached git://github.com/components/jquery.git#2.0.3
bower jquery#&gt;= 1.9.0         validate 2.0.3 against git://github.com/components/jquery.git#&gt;= 1.9.0
bower jquery#~2.0.3            install jquery#2.0.3

jquery#2.0.3 bower_components\jquery

~ D:\workspace\javascript\nodejs-bower&gt;bower list
bower check-new     Checking for new versions of the project dependencies..
nodejs-bower#0.0.0 D:\workspace\javascript\nodejs-bower
&#9500;&#9472;&#9516; bootstrap#3.0.0-rc1 extraneous
&#9474; &#9492;&#9472;&#9472; jquery#2.0.3
&#9500;&#9472;&#9472; d3#3.2.8
&#9492;&#9472;&#9472; jquery#2.0.3
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">多么智能，一键搞定，这才是高效的开发。</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>7). 查看本地bower已经缓存的类库</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower cache list
bootstrap=git://github.com/twbs/bootstrap.git#3.0.0-rc1
d3=git://github.com/mbostock/d3.git#3.2.8
jquery=git://github.com/components/jquery.git#1.7.2
jquery=git://github.com/components/jquery.git#2.0.3
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>8). 查看D3库信息</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower info d3
d3

  Versions:
    - 3.2.8
    - 3.2.7
    - 3.2.6
    - 3.2.5
    - 3.2.4
    - 3.2.3
    ...
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>9). 查看dojo库的url</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower lookup dojo
dojo git://github.com/dojo/dojo.git
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>10). 用浏览器打开dojo的发布主页</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower home dojo
bower dojo#*                not-cached git://github.com/dojo/dojo.git#*
bower dojo#*                   resolve git://github.com/dojo/dojo.git#*
bower dojo#*                  download https://github.com/dojo/dojo/archive/1.9.1.tar.gz
bower dojo#*                   extract archive.tar.gz
bower dojo#*                  resolved git://github.com/dojo/dojo.git#1.9.1
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">浏览器自动打开：https://github.com/dojo/dojo</p><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>11). 查询包含dojo的类库</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code>
~ D:\workspace\javascript\nodejs-bower&gt;bower search dojo
Search results:

    dojo git://github.com/dojo/dojo.git
    dojox git://github.com/dojo/dojox.git
    dojo-util git://github.com/dojo/util.git
    dojo-bootstrap git://github.com/samvdb/Dojo-Bootstrap
</code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">真是方便实用的技术。</p></div></h2><h2 style="color: #434343; font-family: 'Times New Roman'; line-height: normal; background-color: #ffffff;">5. 用bower提交自己类库</h2><h2><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>1). 生成bower.json配置文件</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> ~ D:\workspace\javascript\nodejs-bower&gt;bower init bower existing      The existing bower.json file will be used and filled in [?] name: nodejs-bower [?] version: 0.0.0 [?] main file: [?] set currently installed components as dependencies? No [?] add commonly ignored files to ignore list? Yes </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">查看生成的文件bower.json</p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> {   "name": "nodejs-bower",   "version": "0.0.0",   "ignore": [     "**/.*",     "node_modules",     "bower_components",     "test",     "tests"   ],   "dependencies": {     "d3": "git://github.com/mbostock/d3.git#~3.2.8",     "jquery": "git://github.com/components/jquery.git#~2.0.3"   } } </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>2). 在github创建一个资源库：nodejs-bower</strong><br /><strong>3). 本地工程绑定github</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> ~ D:\workspace\javascript\nodejs-bower&gt;git init Initialized empty Git repository in D:/workspace/javascript/nodejs-bower/.git/  ~ D:\workspace\javascript\nodejs-bower&gt;git add . ~ D:\workspace\javascript\nodejs-bower&gt;git commit -m "first commit" # On branch master # # Initial commit # # Untracked files: #   (use "git add <file>..." to include in what will be committed) # #       app.js #       bower.json #       bower_components/ #       node_modules/ #       package.json #       public/ #       routes/ #       views/ nothing added to commit but untracked files present (use "git add" to track)  ~ D:\workspace\javascript\nodejs-bower&gt;git remote add origin https://github.com/bsspirit/nodejs-bower  ~ D:\workspace\javascript\nodejs-bower&gt;git push -u origin master Counting objects: 565, done. Delta compression using up to 4 threads. Compressing objects: 100% (516/516), done. Writing objects: 100% (565/565), 803.08 KiB, done. Total 565 (delta 26), reused 0 (delta 0) To https://github.com/bsspirit/nodejs-bower  * [new branch]      master -&gt; master Branch master set up to track remote branch master from origin. </file></code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>4). 注册到bower官方类库</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> ~ D:\workspace\javascript\nodejs-bower&gt;bower register nodejs-bower git@github.com:bsspirit/nodejs-bower.git bower                          convert Converted git@github.com:bsspirit/nodejs-bower.git to git://github.com/bsspirit/n odejs-bower.git bower nodejs-bower#*           resolve git://github.com/bsspirit/nodejs-bower.git#* bower nodejs-bower#*          checkout master bower nodejs-bower#*          resolved git://github.com/bsspirit/nodejs-bower.git#af3ceaac07 Registering a package will make it visible and installable via the registry (https://bower.herokuapp.com), continue? (y/ n)                    y bower nodejs-bower            register git://github.com/bsspirit/nodejs-bower.git  Package nodejs-bower registered successfully! All valid semver tags on git://github.com/bsspirit/nodejs-bower.git will be available as versions. To publish a new version, you just need release a valid semver tag.  Run bower info nodejs-bower to list the package versions. </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>5). 查询我们自己的包</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> D:\workspace\javascript\nodejs-bower&gt;bower search nodejs-bower Search results:      nodejs-bower git://github.com/bsspirit/nodejs-bower.git </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;"><strong>6). 安装我们自己的包</strong></p><pre style="color: #4d4d4f; font-weight: normal; line-height: normal; background-color: #ffffff;"><code> D:\workspace\javascript\nodejs-bower&gt;bower install nodejs-bower bower nodejs-bower#*            cached git://github.com/bsspirit/nodejs-bower.git#af3ceaac07 bower nodejs-bower#*          validate af3ceaac07 against git://github.com/bsspirit/nodejs-bower.git#* bower nodejs-bower#*           install nodejs-bower#af3ceaac07  nodejs-bower#af3ceaac07 bower_components\nodejs-bower &#9500;&#9472;&#9472; d3#3.2.8 &#9492;&#9472;&#9472; jquery#2.0.3 </code></pre><p style="color: #4d4d4f; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: normal; background-color: #ffffff;">其实模块化，版本依赖，开发类库，发布类库，安装类库，都是一条命令！还能再简单一点么！快把项目模块化，然后分享给大家吧！！未来是属于开发者的。</p></h2><h2></h2><img src ="http://www.blogjava.net/jjshcc/aggbug/420079.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2014-11-14 13:50 <a href="http://www.blogjava.net/jjshcc/archive/2014/11/14/420079.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>   nodejs、npm、express安装</title><link>http://www.blogjava.net/jjshcc/archive/2014/11/13/420000.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Thu, 13 Nov 2014 01:53:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2014/11/13/420000.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/420000.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2014/11/13/420000.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/420000.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/420000.html</trackback:ping><description><![CDATA[<div><div>Windows平台下的node.js安装</div><div>直接去nodejs的官网http://nodejs.org/上下载nodejs安装程序，双击安装就可以了</div><div>测试安装是否成功：</div><div>在命令行输入 node &#8211;v 应该可以查看到当前安装的nodejs版本号</div><div></div><div>简单的例子</div><div>写一段简短的代码，保存为helloworld.js，大致看下nodejs是怎么用的。</div><div>如下：该代码主要是创建一个http服务器。</div><div>&nbsp;</div><div>var http = require("http"); &nbsp;</div><div>http.createServer(function(request, response) { &nbsp;</div><div>&nbsp; &nbsp; response.writeHead(200, {"Content-Type": "text/html"}); &nbsp;</div><div>&nbsp; &nbsp; response.write("Hello World!"); &nbsp;</div><div>&nbsp; &nbsp; response.end(); &nbsp;</div><div>}).listen(8080); &nbsp;</div><div>console.log("Server running at http://localhost:8080/"); &nbsp;</div><div></div><div>打开命令行，转到当前文件所存放的路径下，运行 node helloworld.js命令即可</div><div>如果一切正常，可以看到命令行输出：Server running at http://localhost:8080/</div><div>同时，在浏览器输入http://localhost:8080/，可以看到一个写着helloworld的网页。</div><div></div><div></div><div></div><div>安装npm</div><div>npm上有很多优秀的nodejs包，来解决常见的一些问题，比如用node-mysql，就可以方便通过nodejs链接到mysql，进行数据库的操作</div><div>在开发过程往往会需要用到其他的包，使用npm就可以下载这些包来供程序调用</div><div></div><div>a) 如果系统没有安装过Git，可以直接到https://github.com/isaacs/npm下载npm所需要的文件。</div><div>b) 如果有Git 可以使用git下载。</div><div>&nbsp;</div><div>git clone --recursive git://github.com/isaacs/npm.git &nbsp;</div><div>下载到NPM文件后，命令行首先转到npm所在地址，输入以下代码进行安装。</div><div>&nbsp;</div><div>node cli.js install npm -gf &nbsp;</div><div></div><div></div><div>安装Express</div><div>Express是nodejs常用的一个框架。</div><div>a) 全局安装&nbsp;</div><div>&nbsp;</div><div>npm install express -gd &nbsp;</div><div>npm install -g express-generator</div><div>b) 安装在当前文件夹下&nbsp;</div><div>&nbsp;</div><div>npm install express &nbsp;</div><div>安装成功后，命令行会提示 npm info ok</div><div>-g代表安装到NODE_PATH的lib里面，而-d代表把相依性套件也一起安装。如果沒有-g的话会安装目前所在的目录(会建立一个node_modules的文件夹)。</div><div></div><div></div><div>在项目中引用express包</div><div>例：</div><div>&nbsp;</div><div>var express = require('express'); &nbsp;</div><div>var app = module.exports = express.createServer(); &nbsp;</div><div>如果没有安装过express,那么首先需要在当前项目文件夹下安装一个express</div><div>命令行转到当前路径后，运行</div><div>&nbsp;</div><div>npm install express &nbsp;</div><div>安装完成后，可以看到当前目录下多了一个【node_modules】文件夹，下有一个【express】文件夹</div><div>注：项目中引用的包，都会被安装到【node_modules】文件夹</div><div></div><div>用express创建项目</div><div>在命令行中输入【express 项目名称】，就可以在当前文件夹下创建一个新的项目</div><div>如图：</div><div></div><div>包括以下几个文件：</div><div></div><div>用此方法，只是创建了一个空的项目框架，和一个简单的实例程序，运行app.js可以查看（还需要在项目文件目录下，安装jade包，方法类似安装express）</div><div></div><div>参考网站：</div><div>Node.js基础 http://www.infoq.com/cn/master-nodejs</div><div>Node.js 入门教程 http://nodebeginner.org/index-zh-cn.html</div><div>Node.js中文文档 http://cnodejs.org/cman/index.html</div><div>Express文档 http://expressjs.com/guide.html#routing</div><div>CNode社区 http://club.cnodejs.org/</div></div><img src ="http://www.blogjava.net/jjshcc/aggbug/420000.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2014-11-13 09:53 <a href="http://www.blogjava.net/jjshcc/archive/2014/11/13/420000.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自制node.js + npm绿色版</title><link>http://www.blogjava.net/jjshcc/archive/2014/11/12/419974.html</link><dc:creator>Eric_jiang</dc:creator><author>Eric_jiang</author><pubDate>Wed, 12 Nov 2014 07:55:00 GMT</pubDate><guid>http://www.blogjava.net/jjshcc/archive/2014/11/12/419974.html</guid><wfw:comment>http://www.blogjava.net/jjshcc/comments/419974.html</wfw:comment><comments>http://www.blogjava.net/jjshcc/archive/2014/11/12/419974.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jjshcc/comments/commentRss/419974.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jjshcc/services/trackbacks/419974.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><strong style="color: black;">Node.js</strong><a href="http://nodejs.org/" target="_blank" style="color: #006699; text-decoration: none;">官网</a>有各平台的<a name="baidusnap4" style="color: rgb(0, 102, 153);"></a><strong style="color: black; background-color: #ff66ff;">安装</strong>包<a href="http://nodejs.org/download" target="_blank" style="color: #006699; text-decoration: none;">下载</a>，不想折腾的可以直接下载<strong style="color: black; background-color: #ff66ff;">安装</strong>，下面说下windows平台下如何制作绿色版node，以方便迁移。</p><ol style="margin: 0px 0px 20px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><li><span>获取</span><a href="http://nodejs.org/download" target="_blank" style="color: #006699; text-decoration: none;"><span>node.exe</span></a><br /><span>下载Windows Binary版本，不要下载Windows Installer版本，直接放到H:\nodejs\</span></li><li><span>获取</span><a href="http://nodejs.org/dist/npm" target="_blank" style="color: #006699; text-decoration: none;"><span>npm</span></a><span>（Node Package Manage）</span><br /><span>下载最新zip版本，不要下载tgz版本，下载后解压到H:\nodejs\</span></li><li><span>添加环境变量</span><ul style="margin: 0px 0px 20px;"><li><span>NODE_HOME=H:\nodejs</span></li><li><span>NODE_PATH=%NODE_HOME%\node_modules</span></li><li><span>path增加%NODE_HOME%\</span></li></ul></li></ol><p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><span>最终得到文件目录结构H:\nodejs\：</span></p><figure style="margin: 0px -30px 16px; padding: 0px 30px; background-color: #3a3c42; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px;"><pre style="font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; color: #f8f8f2; font-size: 0.8em; white-space: pre-wrap; word-wrap: break-word; padding: 10px 30px; margin: 0px -30px;"><span>. &#9500;&#8212;&#8212; node</span><span style="color: #b58900;">.exe</span><span> &#9500;&#8212;&#8212; npm</span><span style="color: #b58900;">.cmd</span><span> &#9492;&#8212;&#8212; node_modules     &#9492;&#8212;&#8212; npm </span></pre></figure><a id="more" style="color: #006699; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"></a><p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><span>测试一下，出现版本号，说明配置成功：</span></p><figure style="margin: 0px -30px 16px; padding: 0px 30px; background-color: #3a3c42; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px;"><pre style="font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; color: #f8f8f2; font-size: 0.8em; white-space: pre-wrap; word-wrap: break-word; padding: 10px 30px; margin: 0px -30px;"><span style="color: #93a1a1; font-style: italic;">node</span> <span style="color: #268bd2;">-</span><span style="color: #268bd2;">-</span><span style="color: #93a1a1; font-style: italic;">version</span> <span style="color: #93a1a1; font-style: italic;">npm</span> <span style="color: #268bd2;">-</span><span style="color: #268bd2;">-</span><span style="color: #93a1a1; font-style: italic;">version </span></pre></figure><blockquote style="margin: 0px -30px 20px; padding: 15px 20px 1px 40px; font-style: italic; background-color: rgba(0, 0, 0, 0.0588235); color: #222222; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px;"><p style="margin: 0px 0px 20px; padding: 0px;"><span>至此，绿色版制作完毕，您已经可以正常使用node/npm，也可以迁移到其他机器使用。</span></p></blockquote><p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><span>下面使用npm命令</span><strong style="color: black;">安装</strong>第三方模块，使用方法可以查看：</p><figure style="margin: 0px -30px 16px; padding: 0px 30px; background-color: #3a3c42; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px;"><pre style="font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; color: #f8f8f2; font-size: 0.8em; white-space: pre-wrap; word-wrap: break-word; padding: 10px 30px; margin: 0px -30px;"><span style="color: #268bd2;">npm</span><span> -h </span><span style="color: #268bd2;">npm</span><span> install -h </span><span style="color: #268bd2;">npm</span><span> help install </span></pre></figure><p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><span>默认</span><strong style="color: black;">安装</strong>仓库是<a href="https://registry.npmjs.org/" target="_blank" style="color: #006699; text-decoration: none;">https://registry.npmjs.org</a>，查找包可以到这里<a href="http://search.npmjs.org/" target="_blank" style="color: #006699; text-decoration: none;">http://search.npmjs.org</a>，一切都很像maven。有两种<strong style="color: black; background-color: #ff66ff;">安装</strong>模式可选，参考<a href="http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation" target="_blank" style="color: #006699; text-decoration: none;">npm 1.0 Global vs Local installation</a>：<br /><span style="color: #222222; font-weight: 700;">locally</span><br />默认是locally模式，<strong style="color: black; background-color: #ff66ff;">安装</strong>到当前命令的执行目录。在其他位置执行xxx会报<em>&#8220;xxx不是内部或外部命令，也不是可运行的程序&#8221;</em>。</p><figure style="margin: 0px -30px 16px; padding: 0px 30px; background-color: #3a3c42; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px;"><pre style="font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; color: #f8f8f2; font-size: 0.8em; white-space: pre-wrap; word-wrap: break-word; padding: 10px 30px; margin: 0px -30px;"><span style="color: #268bd2;">npm</span><span> install xxx </span></pre></figure><p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><span style="color: #222222; font-weight: 700;">globally</span><br /><span>-g参数代表全局方式，使用全局模式会</span><strong style="color: black;">安装</strong>到<em>H:\nodejs\node_modules\</em>中的xxx下。</p><figure style="margin: 0px -30px 16px; padding: 0px 30px; background-color: #3a3c42; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px;"><pre style="font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; color: #f8f8f2; font-size: 0.8em; white-space: pre-wrap; word-wrap: break-word; padding: 10px 30px; margin: 0px -30px;"><span style="color: #268bd2;">npm</span><span> install xxx -g </span></pre></figure><p style="margin: 0px 0px 20px; padding: 0px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><span>你可以重新设置prefix位置，方法有二：</span></p><ul style="margin: 0px 0px 20px; color: #555555; font-family: 'Droid Serif', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 15px; line-height: 24px; text-align: justify; background-color: #dedede;"><li><span>重新设置prefix的位置：npm config set prefix "H:\hexo"</span></li><li><span>或直接修改 『当前用户』.npmrc 文件，添加prefix = H:\hexo</span></li></ul><img src ="http://www.blogjava.net/jjshcc/aggbug/419974.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jjshcc/" target="_blank">Eric_jiang</a> 2014-11-12 15:55 <a href="http://www.blogjava.net/jjshcc/archive/2014/11/12/419974.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>