DIV CSS 佈局教程網

Node.js 入門:從 Hello World 到 HTTP 服務器
編輯:關於JavaScript     

Node.js 是伺服器端 JavaScript 的入門好選擇。從最簡單的命令列輸出開始,再逐步建立 HTTP 服務,可以很快掌握基本流程。

第一個命令列程式

var sys = require("sys");
sys.puts("Hello world");

在命令列執行 node hello.js,即可看到輸出。

第一個網頁程式

var http = require("http");
http.createServer(function (request, response) {
  response.writeHead(200, { "Content-Type": "text/html" });
  response.write("Hello World!");
  response.end();
}).listen(8080);

啟動後,用瀏覽器訪問 https://localhost:8080/,就能看到 Hello World。

Buffer 與編碼

Node.js 提供 Buffer 類別,可用於處理二進位資料與字串編碼。常見編碼包含 ascii、utf8 與 binary。

這些範例看似簡單,卻已涵蓋 Node.js 最基本的執行方式、模組載入、HTTP 回應與資料處理概念。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved