首页 > 代码库 > node.js小结 2

node.js小结 2

下载node安装npm什么的就不说了

 

 

入门总结

http://www.cnblogs.com/Darren_code/archive/2011/10/31/nodejs.html

 

进入node_HOME目录

test.js

/*var hello = require(‘./hello.js‘);console.log(‘hello.location : ‘ + hello.location)console.log(‘hello.location : ‘ + hello.name)hello.showLog();*/    var fs = require(fs);  console.log(11);    var hello = require(./hello.js);  console.log(22);  fs.readFile(./hello.js, function (err, data) {    if (err) throw err;    console.log(successfully);  });  console.log(async);var events = require("events");   var emitter = new events.EventEmitter();   emitter.on("myEvent", function(msg) {     console.log(msg);   });   emitter.emit("myEvent", "Hello World.");var fs = require(fs);  fs.unlink(./hello.js, function (err) {    if (err) throw err;    console.log(successfully deleted myModule.js);  });

 

hello.js

  console.log(00);      var name = "Darren";  this.location = "Beijing";  this.showLog = function(){      console.log(Hi Darren.)  };

 

输出:

11
00
22
async
Hello World.
successfully deleted myModule.js
successfully

 

基本概念的解析:

模块:其实就相当于一个js类, 通过require(路径)方式创建并引入一个模块

异步:

 

node各内置模块及其api

sys

fs

http

agent

url

path

file

events 

 

 

 

 http://nodejs.iteye.com/blog/1587019

通常, js 通过eval 组装 服务端传来的 json字符串成 js对象 -- 服务端有另外的json解释器

var aa = JSON.parse("{\"a\":\"c\"}");   JSON 也是js内置的

 

 

服务端对浏览器的请求处理通常是: 每个请求建立一个线程。。。? 

 

 

一定要明白你需要解决的问题是什么,基于此采取最佳解决方案,而不是基于你当下所掌握的技能来解决遇到的问题

 

node的优势:

无阻塞(异步)、

高并发(异步、基于事件的i/o)

构建可伸缩的网络应用(单线程_ 传统web服务器的一个请求一个线程,即多线程)

 

 

 

 

非常棒的文章:

http://www.cnblogs.com/liusuqi/p/3735491.html