node getting start
TRANSCRIPT
![Page 1: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/1.jpg)
Node.js Getting StartBy @朴灵
12年7月19日星期四
![Page 2: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/2.jpg)
http://www.alibabatech.org/gprofile/37
12年7月19日星期四
![Page 3: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/3.jpg)
Install Node.js
• 下载安装(http://nodejs.org/#download)
• node -v
12年7月19日星期四
![Page 4: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/4.jpg)
Hello world!
node
> console.log(‘hello world!’);
12年7月19日星期四
![Page 5: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/5.jpg)
Hello world!
var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(1337);
console.log('Server running at http://127.0.0.1:1337/');
12年7月19日星期四
![Page 6: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/6.jpg)
NPM
• npm install connect
• npm install connect --registry=http://registry.npm.tbdata.org
• npm ls
• npm config ls
• npm publish .
12年7月19日星期四
![Page 7: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/7.jpg)
Module// hello.jsexports.helloWorld = function () { console.log('Hello World!');};
// example.jsvar hello = require(‘./hello’);hello.helloWorld();
12年7月19日星期四
![Page 8: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/8.jpg)
Package
• npm init
12年7月19日星期四
![Page 9: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/9.jpg)
小结
• 安装• Hello world
• NPM & CommonJS Module/Package
12年7月19日星期四
![Page 10: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/10.jpg)
Web Server
var connect = require('connect');
var app = connect();app.use(connect.static("./public")));app.use(connect.directory("./public"));app.listen(8001);console.log("Running at http://localhost:8001");
12年7月19日星期四
![Page 11: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/11.jpg)
Connect中间件
// 使用中间件技术app.use(function (req, res, next) {
});
12年7月19日星期四
![Page 12: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/12.jpg)
目录结构• /dispatch.js
• /app.js
• /routes.js
• /controllers/ 业务逻辑相关
• /views/
• /common/ 跟业务相关的公共模块
• /proxy/ 数据访问代理层
• /lib/ 跟业务无关的公共模块
• /assets/ 静态文件
• /test/ 测试
• /bin/ 可执行文件
• /tools/ 工具
• /node_moudules
12年7月19日星期四
![Page 13: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/13.jpg)
Node-Clustervar cluster = require('node-cluster');var master = new cluster.Master();master.register(8080, 'app.js');master.dispatch(); var cluster = require('node-cluster');
var worker = new cluster.Worker();var connect = require('connect');var app = connect();app.use(connect.static("./public")));app.use(connect.directory("./public"));worker.ready(function (socket) { app.emit('connection', socket);});
12年7月19日星期四
![Page 14: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/14.jpg)
本地NPM
12年7月19日星期四
![Page 15: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/15.jpg)
本地NPM
官方NPM
12年7月19日星期四
![Page 16: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/16.jpg)
本地NPM
官方NPM
12年7月19日星期四
![Page 17: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/17.jpg)
本地NPM
官方NPM单向同步
12年7月19日星期四
![Page 18: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/18.jpg)
本地NPM
本地NPM 官方NPM单向同步
12年7月19日星期四
![Page 19: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/19.jpg)
本地NPM
本地NPM 官方NPM单向同步
12年7月19日星期四
![Page 20: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/20.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
12年7月19日星期四
![Page 21: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/21.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
12年7月19日星期四
![Page 22: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/22.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
私有模块
12年7月19日星期四
![Page 23: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/23.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
私有模块
12年7月19日星期四
![Page 24: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/24.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
私有模块
12年7月19日星期四
![Page 25: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/25.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
私有模块 公有模块
12年7月19日星期四
![Page 26: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/26.jpg)
本地NPM
本地NPM 官方NPM
项目
单向同步
私有模块 公有模块
12年7月19日星期四
![Page 27: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/27.jpg)
常用模块• connect/ejs
• mongoskin/redis/mredis
• eventproxy
• node-cluster
• iconv
• mocha/should/rewire
• node-dev
12年7月19日星期四
![Page 28: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/28.jpg)
阿里专用模块• taobaosession
• taobao-cdn
• taobaostatus
• sm-client
• hsf
• itier
• kfc-client
12年7月19日星期四
![Page 29: Node getting start](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f5f8ab4c9058a148b468a/html5/thumbnails/29.jpg)
References
• http://www.infoq.com/cn/master-nodejs
• http://www.windowsazure.com/en-us/develop/nodejs/tutorials/getting-started/
• http://nodeguide.com/beginner.html
• http://www.i-programmer.info/programming/javascript/3400-getting-started-with-nodejs.html
12年7月19日星期四