標籤:style blog http color io 問題
下載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