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

出自福留子孫
跳轉到: 導覽搜尋
二、共筆白板
二、共筆白板
第 33 行: 第 33 行:
  
 
在 DS720+ 上執行:
 
在 DS720+ 上執行:
1. 先裝 Node.js v18
+
#先裝 Node.js v18
2. 建 web 下建 node_socket 資料夾
+
#建 web 下建 node_socket 資料夾
3. 上傳 Socket_test.js,Socket_test.html 到 node_socket 資料夾
+
#上傳 Socket_test.js,Socket_test.html 到 node_socket 資料夾
4. 取得 root 權限後在 node_socket 資料夾 執行「npm install express」
+
#取得 root 權限後在 node_socket 資料夾 執行「npm install express」
5. 在伺服器執行「node Socket_test.js」時,會需要一連串的 node_modules ,從 etherpad 拷過來後,終於可以執行
+
#在伺服器執行「node Socket_test.js」時,會需要一連串的 node_modules ,從 etherpad 拷過來後,終於可以執行
6. 在瀏覽器中跑「http://twees.info/node_socket/Socket_test.html」會動,但和後端連不起來
+
#在瀏覽器中跑「http://twees.info/node_socket/Socket_test.html」會動,但和後端連不起來
7. 可見從 etherpad 拷過來的 node_modules ,並不符合本例中的需求,須帶芸伍電腦中的 node_modules 過來
+
#可見從 etherpad 拷過來的 node_modules ,並不符合本例中的需求,須帶芸伍電腦中的 node_modules 過來

2023年3月7日 (二) 08:03的修訂版本

一、示例一

DS218+ /web 下,執行「node index.js」會出

Hello, World!
Server running at http://localhost:3000/

index.js 程式碼如下:

// 引入 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');
});

// 啟動伺服器,並監聽 3000 port 的連線
server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/'); // 當伺服器啟動後,在命令列中印出訊息
});

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

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

二、共筆白板

  1. http://192.168.4.2/eBoard.html
  2. http://192.168.4.2/eBoard.js
    node eBoard.js 會跑 V8 然後被中斷

在 DS720+ 上執行:

  1. 先裝 Node.js v18
  2. 建 web 下建 node_socket 資料夾
  3. 上傳 Socket_test.js,Socket_test.html 到 node_socket 資料夾
  4. 取得 root 權限後在 node_socket 資料夾 執行「npm install express」
  5. 在伺服器執行「node Socket_test.js」時,會需要一連串的 node_modules ,從 etherpad 拷過來後,終於可以執行
  6. 在瀏覽器中跑「http://twees.info/node_socket/Socket_test.html」會動,但和後端連不起來
  7. 可見從 etherpad 拷過來的 node_modules ,並不符合本例中的需求,須帶芸伍電腦中的 node_modules 過來