Node.js初探之hello world
由于跑到另一個(gè)城市,手頭沒電腦,dom framework不能如期發(fā)布,趁此學(xué)習(xí)一些新東西。這時(shí)期最迫切的需要是尋求一個(gè)超輕量的后端來架起我的框架,于是觸爪伸向傳說中的Server-Side Javascrpt。后端JS最出名無疑是Ryan Dahl的node.js,另一個(gè)是aptana IDE提供商搞出的jaxer。
首先下載node.js,然后解壓到E盤,改名為node,然后開始菜單輸入cmd,用cd命令切換到nodejs的解壓目錄:

***個(gè)例子:hello world。
在node目錄下建立hello.js文件,然后在里面輸入:
- var sys = require("sys");
- sys.puts("Hello world");
然后我們?cè)诿_(tái)中輸入命令node hello.js,就能看到命名臺(tái)輸出結(jié)果Hello world。
第二個(gè)例子:hello world2。
好了,這次我們?cè)噺挠斡[器中輸出hello world。在node目錄下建立http.js,然后輸入:
- var sys = require("sys"),
- http = require("http");
- http.createServer(function(request, response) {
- response.sendHeader(200, {"Content-Type": "text/html"});
- response.write("Hello World!");
- response.close();
- }).listen(8080);
- sys.puts("Server running at http://localhost:8080/");
然后我們?cè)诿_(tái)中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/


第三個(gè)例子:hello world2。
node.js提供一個(gè)Buffer類用于轉(zhuǎn)換不同編碼的字符串。目前支持三種類型:'ascii','utf8'與'binary'。詳見這里
- var Buffer = require('buffer').Buffer,
- buf = new Buffer(256),
- len = buf.write('\u00bd + \u00bc = \u00be', 0);
- console.log(len + " bytes: " + buf.toString('utf8', 0, len));
第四個(gè)例子:hello world3。
- //synopsis.js
- //synopsis 摘要, 梗概,大綱
- var http = require('http');
- http.createServer(function (request, response) {
- response.writeHead(200, {'Content-Type': 'text/plain'});
- response.end('Hello World\n');
- }).listen(8124);
- console.log('Server running at http://127.0.0.1:8124/');
前臺(tái)地址欄:http://localhost:8124/
出處:http://www.cnblogs.com/rubylouvre/
【編輯推薦】