這些天有事沒事的稍微看看Node.js,覺得還是挺有意思的,這里自己做一個簡單的例子,起碼能讓沒玩過的人覺得Node.js沒那么神秘。
1.下載安裝Node.js for windows,安裝。http://nodejs.org
2.CMD到安裝根目錄下安裝模塊,express:npm
install
express,還可以安裝mysql:
npm
install
mysql,以及mangodb:npm install mongodb
3.這樣環境就做好了(簡單示例就不使用其他的IDE了),在根目錄下新建一個hello.js文件,復制代碼如下:
var http = require('http'); var url = require('url'); http.createServer(function (req, res) { var path = url.parse(req.url).pathname; var dt=new Date(); res.writeHead(200, {'Content-Type': 'text/plain'}); res.write("Hello,World ! \n"+dt.getTime()); res.end();}).listen(8888, "127.0.0.1"); console.log('access:http://localhost:8888');
4.啟動我們的實例,CMD到hello.js目錄輸入(Node的子目錄下可直接使用node命令,根目錄執行文件須使用完整的路徑文件名C:\Program Files\nodejs\JS>node "C:\Program Files\nodejs\JS\hw.js"):
node hello.js
在瀏覽器中輸入地址:localhost:8888
刷新頁面看到時間的變化。
其他的不多說,代碼解釋有很多文章介紹,這個是玩Node.js要了解的最基本的東西。
后面還會有文章介紹,敬請期待。