分享平台构建之旅13. NodeJS, 啥?
• server-side V8
• event-driven 事件驱动
• non-blocking I/O 无阻塞的IO
• single threaded, efficient 单线程
• freaking fast 真XX快
15. a Simple Server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
19. Nginx, Who?
• HTTP 和 反向代理 服务器
• IMAP/POP3/SMTP 代理服务器
• Non-Blocking 无阻塞
20. Nginx, Who?
• HTTP 和 反向代理 服务器
• IMAP/POP3/SMTP 代理服务器
• Non-Blocking 无阻塞
• Event-Driven 事件驱动
21. Nginx, Who?
• HTTP 和 反向代理 服务器
• IMAP/POP3/SMTP 代理服务器
• Non-Blocking 无阻塞
• Event-Driven 事件驱动
• Single threaded 单线程
22. Nginx, Who?
• HTTP 和 反向代理 服务器
• IMAP/POP3/SMTP 代理服务器
• Non-Blocking 无阻塞
• Event-Driven 事件驱动
• Single threaded 单线程
• Resource efficient 高效
23. Nginx, Who?
• HTTP 和 反向代理 服务器
• IMAP/POP3/SMTP 代理服务器
• Non-Blocking 无阻塞
• Event-Driven 事件驱动
• Single threaded 单线程
• Resource efficient 高效
• Highly modular 高度模块化
31. Work Together
fenxinag.taobao.net
:80
Nginx
proxy to:8000
NodeJS Server
32. Work Together
fenxinag.taobao.net
:80
Nginx
proxy to:8000
NodeJS Server
33. Work Together
fenxinag.taobao.net
:80
Nginx
proxy to:8000
NodeJS Server
34. Work Together
fenxinag.taobao.net
:80
Static
Nginx
File
proxy to:8000
NodeJS Server
35. Work Together
fenxinag.taobao.net
:80
Static
Nginx
File
proxy to:8000
NodeJS Server
36. Work Together
fenxinag.taobao.net
:80
Static
Nginx
File
proxy to:8000
NodeJS Server
37. 配置nginx很简单
server
{
listen 80;
server_name fenxiang.taobao.net;
location / {
proxy_pass http://localhost:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
38. nginx dealing with the static files
server
{
listen 80;
server_name fenxiang.taobao.net;
location ~* .(png|jpg|gif|zip|ppt|rar|pdf|js|css|less)$ {
root /home/wwwroot/fenxiang/public;
}
location / {
proxy_pass http://localhost:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
42. • noSQL | Document Based 文档型数据库
• store in JSON; query in json.
星系 发的故事
43. • noSQL | Document Based 文档型数据库
• store in JSON; query in json.
• Speak Javascript 不说鸟语
星系 发的故事
52. a Simple Server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
原生的HTTPServer比较
弱
只提供非常有限的功能
不过,如何去扩展我们
的server 。
53. a Simple Server
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
回调
res.end('Hello Worldn');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
原生的HTTPServer比较
弱
只提供非常有限的功能
不过,如何去扩展我们
的server 。
55. • logger • methodOverride
• csrf • responseTime
• compress • staticCache
• basicAuth • static
• bodyParser • directory
• json • vhost
• urlencoded • favicon
• multipart • limit
• cookieParser • query
• session • errorHandler
• cookieSession
63. what is middleware?
session
cookie
parser
query
请求 响应
回调
进入 发送
function (req, res) {
req.query 对象 ✌
}
64. what is middleware?
session
cookie
parser
query
请求 响应
回调
进入 发送
function (req, res) {
req.query 对象 ✌
req.session 对象 ✌
req.cookie 对象 ✌
}
65. what is middleware?
compress
session
cookie
parser
query
请求 响应
回调
进入 发送
function (req, res) {
req.query 对象 ✌
req.session 对象 ✌
req.cookie 对象 ✌
}
66. what is middleware?
compress
session
cookie
parser
query
请求
回调 响应
发送
进入
function (req, res) {
req.query 对象 ✌
req.session 对象 ✌
req.cookie 对象 ✌
}
72. • Web 发框架
• 基于Connect
• 轻量高效, 满足日常所需
• 易扩展
• 路由 Routers
78. 分享平台提供的资源
分享 * 分享会
分享会 数据模式
{
subject : {type:String, required:true},
shares : [ { type: Schema.ObjectId, ref: 'share' } ],
...
}
81. 类 Resouseful
操作 键 HTTP方法 路径
新建 new GET /share/new
创建 create POST /share
更新 update PUT /share/000001
获取 show GET /share/000001
索引 index GET /share
删除 delete DELETE /share/000001
91. • MVC
• Backbone - mustache
• 替代方案 Kissy mvc,Kissy Template
92. • MVC
• Backbone - mustache
• 替代方案 Kissy mvc,Kissy Template
• Bootstrap from twitter
93. • MVC
• Backbone - mustache
• 替代方案 Kissy mvc,Kissy Template
• Bootstrap from twitter
• LESS
98. 版本管理
• git
• 分支管理
• master (当前部署)
99. 版本管理
• git
• 分支管理
• master (当前部署)
• dev ( 发分支,待部署)
100. 版本管理
• git
• 分支管理
• master (当前部署)
• dev ( 发分支,待部署)
• feature1 (特性分支)
101. 版本管理
• git
• 分支管理
• master (当前部署)
• dev ( 发分支,待部署)
• feature1 (特性分支)
• feature2 (特性分支)
102. 版本管理
• git
• 分支管理
• master (当前部署)
• dev ( 发分支,待部署)
• feature1 (特性分支)
• feature2 (特性分支)
• git hook 自动更新和部署
103. 通过 hooks 自动更新和部署
#!/bin/sh
//进入部署文件夹
cd ~/ratting-app/
unset GIT_DIR
//更新文件
git pull
//安装依赖
npm install -d
//重启应用
forever restart /home/git/app/server.js
echo success...
exec git-update-server-info
112. 相 链接
• nodejs http://nodejs.org
• Expressjs http://expressjs.com/
• Connect http://www.senchalabs.org/connect/
• MongoDB http://www.mongodb.org/
• Mongoose http://mongoosejs.com/
• Nginx http://nginx.org/
• momentjs http://momentjs.com
• 深入浅出REST http://www.infoq.com/cn/articles/rest-introduction
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n