Node.js是一套用來編寫高性能網(wǎng)絡服務器的JavaScript工具包,一系列的變化由此開始。一個可以快速構建網(wǎng)絡服務及應用的平臺。該平臺的構建是基于Chrome's JavaScript runtime,也就是說,實際上它是對GoogleV8引擎(應用于Google Chrome瀏覽器)進行了封裝。V8引 擎執(zhí)行Javascript的速度非常快,性能非常好。
Node對一些特殊用例進行了優(yōu)化,提供了替代的API,使得V8在非瀏覽器環(huán)境下運行得更好。例如,在服務器環(huán)境中,處理二進制數(shù)據(jù)通常是必不可少的,但Javascript對此支持不足,因此,V8.Node增加了Buffer類,方便并且高效地 處理二進制數(shù)據(jù)。因此,Node不僅僅簡單的使用了V8,還對其進行了優(yōu)化,使其在各環(huán)境下更加給力。
如果是在Windows下,那就需要安裝MinGW以獲得一個仿POSIX的環(huán)境。在Node中,Http是首要的。Node為創(chuàng)建http服務器作了優(yōu)化,所以在網(wǎng)上看到的大部分示例和庫都是集中在web上(http框架、模板庫等)。
node.js安裝方法在網(wǎng)上可以搜出一大堆出來,只是看起來各種方法都不一樣,這就讓小弟迷糊了,索性各種方法都去試,但每種方法都給搞得云里霧里到最后卻還是沒成功,很是苦惱,F(xiàn)在才明白過來,之所以那么多方法,全是因為node.js這個新東西太火,發(fā)展太快。以前是因為要安裝模擬環(huán)境所以才搞那么復雜。今天小弟就分享一個超簡單的安裝方法。
第一步:http://www.innovatechautomation.com/soft/52260.html下載安裝,我下載下來的是node-v0.8.15-x86.msi這個版本的,然后雙擊安裝就好了;
第二步:環(huán)境變量配置,
找到 PATH 變量,在里面添加 C:\Program Files\nodejs
添加完PATH后 再新建一個變量NODE_PATH 變量值 C:\Program Files\nodejs\node_modules
到這里就算是安裝配置完成了,下面我們來測試一下。
第三步:測試
在D盤新建一個nodejs文件夾,在這個文件夾下創(chuàng)建一個ceshi.js文件添加以下js內容:
<SPAN style="COLOR: #1e347b" class=kwd>var</SPAN><SPAN style="COLOR: #48484c" class=pln> http </SPAN><SPAN style="COLOR: #93a1a1" class=pun>=</SPAN><SPAN style="COLOR: #1e347b" class=kwd>require</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #dd1144" class=str>'http'</SPAN><SPAN style="COLOR: #93a1a1" class=pun>);</SPAN><SPAN style="COLOR: #48484c" class=pln>
http</SPAN><SPAN style="COLOR: #93a1a1" class=pun>.</SPAN><SPAN style="COLOR: #48484c" class=pln>createServer</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #1e347b" class=kwd>function</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #48484c" class=pln>req</SPAN><SPAN style="COLOR: #93a1a1" class=pun>,</SPAN><SPAN style="COLOR: #48484c" class=pln> res</SPAN><SPAN style="COLOR: #93a1a1" class=pun>)</SPAN><SPAN style="COLOR: #93a1a1" class=pun>{</SPAN><SPAN style="COLOR: #48484c" class=pln>
res</SPAN><SPAN style="COLOR: #93a1a1" class=pun>.</SPAN><SPAN style="COLOR: #48484c" class=pln>writeHead</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #195f91" class=lit>200</SPAN><SPAN style="COLOR: #93a1a1" class=pun>,</SPAN><SPAN style="COLOR: #93a1a1" class=pun>{</SPAN><SPAN style="COLOR: #dd1144" class=str>'Content-Type'</SPAN><SPAN style="COLOR: #93a1a1" class=pun>:</SPAN><SPAN style="COLOR: #dd1144" class=str>'text/plain'</SPAN><SPAN style="COLOR: #93a1a1" class=pun>});</SPAN><SPAN style="COLOR: #48484c" class=pln>
res</SPAN><SPAN style="COLOR: #93a1a1" class=pun>.</SPAN><SPAN style="COLOR: #1e347b" class=kwd>end</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #dd1144" class=str>'Hello node.js\n'</SPAN><SPAN style="COLOR: #93a1a1" class=pun>);</SPAN><SPAN style="COLOR: #93a1a1" class=pun>}).</SPAN><SPAN style="COLOR: #48484c" class=pln>listen</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #195f91" class=lit>8000</SPAN><SPAN style="COLOR: #93a1a1" class=pun>,</SPAN><SPAN style="COLOR: #dd1144" class=str>'127.0.0.1'</SPAN><SPAN style="COLOR: #93a1a1" class=pun>);</SPAN><SPAN style="COLOR: #48484c" class=pln>
console</SPAN><SPAN style="COLOR: #93a1a1" class=pun>.</SPAN><SPAN style="COLOR: #48484c" class=pln>log</SPAN><SPAN style="COLOR: #93a1a1" class=pun>(</SPAN><SPAN style="COLOR: #dd1144" class=str>'Server running at http://127.0.0.1:8000/'</SPAN><SPAN style="COLOR: #93a1a1" class=pun>);<BR></SPAN>
保存為 ceshi.js后放一邊,下一步點擊系統(tǒng)的開始菜單運行 cmd命令:
d:
D:\> cd nodejs
D:\>nodejs>node ceshi.js
然后出現(xiàn):Server running at http://127.0.0.1:8000/ 就是成功了
最后在瀏覽器里打開http://localhost:8000 有驚喜哦!!!!!!
ceshi.js下載:http://pan.baidu.com/share/link?shareid=165302&uk=85241834