Node.js/基本範例程式:修訂版本之間的差異

出自福留子孫
跳轉到: 導覽搜尋
第 1 行: 第 1 行:
 
[[分類:Node.js]]
 
[[分類:Node.js]]
 
 
 
===一、示例一===
 
===一、示例一===
 +
啟動伺服器,並監聽 3000 port 的連線
 
<pre>// 引入 Node.js 的核心模組
 
<pre>// 引入 Node.js 的核心模組
 
const http = require('http');
 
const http = require('http');
第 12 行: 第 11 行:
 
   res.setHeader('Content-Type', 'text/plain');
 
   res.setHeader('Content-Type', 'text/plain');
 
   res.end('Hello, World!\n');
 
   res.end('Hello, World!\n');
});</pre>
+
});
 +
 
 +
// 引入內建的 `console` 模組
 +
const console = require('console');
 +
 
 +
// 呼叫 `console.log` 方法,在終端機中輸出一段文字
 +
console.log('Hello, World!');</pre>

2023年2月14日 (二) 10:56的修訂版本

一、示例一

啟動伺服器,並監聽 3000 port 的連線

// 引入 Node.js 的核心模組
const http = require('http');

// 建立一個 HTTP 伺服器
const server = http.createServer((req, res) => {
  // 設定 HTTP 回應的狀態碼、標頭和內容
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

// 引入內建的 `console` 模組
const console = require('console');

// 呼叫 `console.log` 方法,在終端機中輸出一段文字
console.log('Hello, World!');