Dark mode
Introduction to Node.js
What is Node.js?
Node.js เป็น JavaScript runtime environment ที่ทำงานนอกเบราว์เซอร์ ทำให้สามารถใช้ JavaScript ในการพัฒนาแอปพลิเคชันฝั่ง server ได้ โดย Node.js ใช้ V8 JavaScript engine ของ Google (เดียวกับที่ใช้ใน Chrome) เพื่อประมวลผลโค้ด JavaScript
Example: Simple HTTP Server
javascript
// สร้าง HTTP server อย่างง่าย
import http from "http";
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World\n");
});
server.listen(3000, "127.0.0.1", () => {
console.log("Server running at http://127.0.0.1:3000/");
});
Node.js มีระบบ event-driven และ non-blocking I/O ทำให้เหมาะสำหรับแอปพลิเคชันที่ต้องการประสิทธิภาพสูงและการทำงานแบบ real-time เช่น API servers, streaming applications และ microservices
How Node.js Works
V8 Compiles JavaScript to Machine Code
V8 Engine คอมไพล์โค้ด JavaScript เป็น machine code ด้วยเทคนิค Just-In-Time (JIT) compilation ซึ่งทำให้โค้ด JavaScript สามารถทำงานได้โดยเร็วและมีประสิทธิภาพ
กระบวนการทำงานของ V8:
- Parsing: แปลงโค้ด JavaScript เป็น Abstract Syntax Tree (AST)
- Ignition: Interpreter ที่แปลง AST เป็น bytecode และเริ่มทำงานทันที
- TurboFan: Optimizer ที่วิเคราะห์โค้ดที่ทำงานบ่อยและคอมไพล์เป็น optimized machine code
javascript
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // 8
Event Loop Manages Non-Blocking I/O
Event Loop เป็นระบบจัดการงานแบบ non-blocking I/O ผ่านระบบ event-driven ซึ่งทำให้ Node.js สามารถรองรับการเชื่อมต่อจำนวนมากพร้อมกันได้อย่างมีประสิทธิภาพ
ขั้นตอนการทำงานของ Event Loop:
- Timers: ตรวจสอบ callbacks จาก setTimeout() และ setInterval()
- Pending callbacks: ดำเนินการกับ I/O callbacks ที่ถูกเลื่อนจากรอบก่อนหน้า
- Poll: รับ I/O events ใหม่และดำเนินการกับ callbacks ที่เกี่ยวข้อง
- Check: ดำเนินการกับ callbacks จาก setImmediate()
- Close callbacks: ดำเนินการกับ close events
javascript
console.log("1: Start");
setTimeout(() => console.log("2: Timeout"), 0);
setImmediate(() => console.log("3: Immediate"));
process.nextTick(() => console.log("4: NextTick"));
console.log("5: End");
// Output มักจะเป็น:
// 1: Start
// 5: End
// 4: NextTick
// 2: Timeout
// 3: Immediate
Libuv Handles I/O Operations
Libuv เป็นไลบรารีที่จัดการกับ asynchronous I/O operations บนระบบปฏิบัติการต่างๆ ซึ่งทำให้ Node.js สามารถรองรับการทำงานแบบ concurrent ได้ด้วย event loop
คุณสมบัติของ Libuv:
- Thread Pool: จัดการกับ blocking I/O operations
- Event Loop: จัดการกับ non-blocking operations
- Cross-platform: ทำงานได้บนหลายระบบปฏิบัติการ (Windows, Linux, macOS)
javascript
const fs = require("fs");
// Non-blocking I/O operation
fs.readFile("example.txt", "utf8", (err, data) => {
if (err) throw err;
console.log("File content:", data);
});
console.log("Reading file... (this shows first)");
Built-in Modules
Node.js มาพร้อมกับโมดูลพื้นฐานที่หลากหลาย ช่วยให้ผู้พัฒนาสามารถทำงานกับระบบไฟล์, เครือข่าย, และอื่นๆ ได้โดยไม่ต้องติดตั้งแพ็คเกจเพิ่มเติม
โมดูลพื้นฐานที่สำคัญ:
- fs: จัดการกับระบบไฟล์ เช่น อ่าน/เขียนไฟล์
- http/https: สร้าง web server และ client
- path: จัดการกับ file paths
- events: สร้างและจัดการ event emitters
- crypto: เข้ารหัสและถอดรหัสข้อมูล
javascript
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello World\n");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000/");
});
Key Features of Node.js
- Asynchronous and Event-Driven: ทุก API ของ Node.js เป็นแบบ asynchronous (non-blocking)
- Single-Threaded but Highly Scalable: ใช้ Event Loop ในการจัดการ request หลายๆ ตัวพร้อมกัน
- Fast in Code Execution: ใช้ V8 JavaScript Engine ของ Google
- No Buffering: แอปพลิเคชัน Node.js ไม่เคย buffer ข้อมูลใดๆ
- Open Source: มี community ที่ใหญ่และแข็งแกร่ง
When to Use Node.js
Node.js เหมาะสำหรับการพัฒนา:
- Real-time Applications: แชท, เกมส์, แอปพลิเคชันที่ต้องอัพเดทแบบ real-time
- API Services: RESTful APIs, GraphQL APIs
- Microservices: ระบบที่แบ่งการทำงานเป็นส่วนย่อยๆ
- Single Page Applications (SPA): Web applications ที่มีการอัพเดทหน้าเว็บโดยไม่ต้องโหลดหน้าใหม่
- Streaming Applications: การประมวลผลไฟล์มีเดียขนาดใหญ่
Getting Started
ติดตั้ง Node.js ได้ที่ เว็บไซต์ทางการ หรือใช้ package manager ต่างๆ เช่น nvm (Node Version Manager) สำหรับการจัดการเวอร์ชัน
ตรวจสอบการติดตั้ง:
bash
node --version
npm --version
เริ่มต้นโปรเจคใหม่:
bash
mkdir my-node-app
cd my-node-app
npm init -y
สร้างไฟล์ app.js
และทดสอบด้วยคำสั่ง:
bash
node app.js
Conclusion
Node.js เป็นแพลตฟอร์มที่ทรงพลังสำหรับการพัฒนาแอปพลิเคชันด้าน server-side โดยใช้ JavaScript ทำให้สามารถใช้ภาษาโปรแกรมมิ่งเพียงภาษาเดียวทั้งฝั่ง frontend และ backend ได้อย่างมีประสิทธิภาพ