
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 快速入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 快速入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - 第一個應用程式
學習一門新的語言或框架時,第一個要編寫的應用程式是 Hello World 程式。此類程式顯示 Hello World 訊息。這說明了該語言的基本語法,並用於測試語言編譯器的安裝是否正確。本章將用 Node.js編寫一個 Hello World 應用程式。
控制檯應用程式
Node.js 具有命令列介面。Node.js 執行時還允許您在瀏覽器外部執行 JavaScript 程式碼。因此,任何 JavaScript 程式碼都可以在 Node.js 可執行檔案的命令終端中執行。
將以下單行 JavaScript 程式碼儲存為 hello.js 檔案。
console.log("Hello World");
在 hello.js 檔案所在的資料夾中開啟 powershell(或命令提示符)終端,然後輸入以下命令:
PS D:\nodejs> node hello.js Hello World
Hello World 訊息將顯示在終端中。
建立 Node.js 應用程式
要使用 Node.js 建立一個“Hello, World!”web 應用程式,您需要以下三個重要元件:
匯入所需模組 - 我們使用 require 指令載入 Node.js 模組。
建立伺服器 - 一個類似於 Apache HTTP Server 的伺服器,它將監聽客戶端的請求。
讀取請求並返回響應 - 在前面步驟中建立的伺服器將讀取客戶端(可以是瀏覽器或控制檯)發出的 HTTP 請求並返回響應。
步驟 1 - 匯入所需模組
我們使用 require 指令載入 http 模組並將返回的 HTTP 例項儲存到 http 變數中,如下所示:
var http = require("http");
步驟 2 - 建立伺服器
我們使用建立的 http 例項並呼叫 http.createServer() 方法來建立一個伺服器例項,然後我們使用與伺服器例項關聯的 listen 方法將其繫結到 3000 埠。向其傳遞一個帶有 request 和 response 引數的函式。
createserver() 方法具有以下語法:
http.createServer(requestListener);
每當伺服器從客戶端接收請求時,requestlistener 引數都是一個執行的函式。此函式處理傳入的請求並形成伺服器響應。
requestlistener 函式從 Node.js 執行時獲取 request HTTP 請求和 response 物件,並返回一個 ServerResponse 物件。
listener = function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/html'}); // Send the response body as "Hello World" response.end('<h2 style="text-align: center;">Hello World</h2>'); };
上述函式將狀態程式碼和 content-type 標頭新增到 ServerResponse 和 Hello World 訊息中。
此函式用作 createserver() 方法的引數。伺服器被設定為偵聽特定埠(讓我們將 3000 指定為埠)上的傳入請求。
步驟 3 - 測試請求和響應
編寫示例實現以始終返回“Hello World”。將以下指令碼儲存為 hello.js。
http = require('node:http'); listener = function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/html response.writeHead(200, {'Content-Type': 'text/html'}); // Send the response body as "Hello World" response.end('<h2 style="text-align: center;">Hello World</h2>'); }; server = http.createServer(listener); server.listen(3000); // Console will print the message console.log('Server running at http://127.0.0.1:3000/');
在 PowerShell 終端中,輸入以下命令。
PS D:\nodejs> node hello.js Server running at http://127.0.0.1:3000/
程式在 localhost 上啟動 Node.js 伺服器,並在 3000 埠進入偵聽模式。現在開啟瀏覽器,輸入 http://127.0.0.1:3000/ 作為 URL。瀏覽器將按預期顯示 Hello World 訊息。
