Contenu connexe Similaire à Node getting start (19) Node getting start3. Install Node.js
• 下载安装(http://nodejs.org/#download)
• node -v
12年7月19日星期四
4. Hello world!
node
> console.log(‘hello world!’);
12年7月19日星期四
5. Hello world!
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337);
console.log('Server running at http://127.0.0.1:1337/');
12年7月19日星期四
6. NPM
• npm install connect
• npm install connect --registry=http://
registry.npm.tbdata.org
• npm ls
• npm config ls
• npm publish .
12年7月19日星期四
7. Module
// hello.js
exports.helloWorld = function () {
console.log('Hello World!');
};
// example.js
var hello = require(‘./hello’);
hello.helloWorld();
12年7月19日星期四
9. 小结
• 安装
• Hello world
• NPM & CommonJS Module/Package
12年7月19日星期四
10. 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日星期四
11. Connect中间件
// 使用中间件技术
app.use(function (req, res, next) {
});
12年7月19日星期四
12. 目录结构
• /dispatch.js
• /app.js
• /routes.js
• /controllers/ 业务逻辑相关
• /views/
• /common/ 跟业务相关的公共模块
• /proxy/ 数据访问代理层
• /lib/ 跟业务无关的公共模块
• /assets/ 静态文件
• /test/ 测试
• /bin/ 可执行文件
• /tools/ 工具
• /node_moudules
12年7月19日星期四
13. Node-Cluster
var 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日星期四
15. 本地NPM
官方NPM
12年7月19日星期四
16. 本地NPM
官方NPM
12年7月19日星期四
17. 本地NPM
单向同步
官方NPM
12年7月19日星期四
18. 本地NPM
单向同步
本地NPM 官方NPM
12年7月19日星期四
19. 本地NPM
单向同步
本地NPM 官方NPM
12年7月19日星期四
20. 本地NPM
单向同步
本地NPM 官方NPM
项目
12年7月19日星期四
21. 本地NPM
单向同步
本地NPM 官方NPM
项目
12年7月19日星期四
22. 本地NPM
单向同步
本地NPM 官方NPM
私有模块 项目
12年7月19日星期四
23. 本地NPM
单向同步
本地NPM 官方NPM
私有模块 项目
12年7月19日星期四
24. 本地NPM
单向同步
本地NPM 官方NPM
私有模块 项目
12年7月19日星期四
25. 本地NPM
单向同步
本地NPM 官方NPM
私有模块 项目 公有模块
12年7月19日星期四
26. 本地NPM
单向同步
本地NPM 官方NPM
私有模块 项目 公有模块
12年7月19日星期四
27. 常用模块
• connect/ejs
• mongoskin/redis/mredis
• eventproxy
• node-cluster
• iconv
• mocha/should/rewire
• node-dev
12年7月19日星期四
28. 阿里专用模块
• taobaosession
• taobao-cdn
• taobaostatus
• sm-client
• hsf
• itier
• kfc-client
12年7月19日星期四
29. 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日星期四