Dark mode
Node.js เบื้องต้น
Node.js เป็นแพลตฟอร์มที่นิยมใช้สำหรับพัฒนาแอปพลิเคชันด้านเซิร์ฟเวอร์ด้วย JavaScript
Key Features
Feature | Description |
---|---|
Runtime Environment | สภาพแวดล้อมสำหรับรัน JavaScript บนเซิร์ฟเวอร์ |
Event-driven Architecture | ทำงานแบบ event-driven และ non-blocking I/O |
Asynchronous Programming | รองรับการเขียนโปรแกรมแบบ asynchronous |
Single-threaded with Event Loop | ใช้ single thread พร้อม event loop |
Cross-platform | ทำงานได้บน Windows, Linux และ macOS |
NPM (Node Package Manager) | ระบบจัดการแพ็กเกจที่ใหญ่ที่สุด |
Built-in Modules | มีโมดูลพื้นฐานเช่น fs, http, path ให้ใช้งาน |
Installation
ส่วนนี้จะแนะนำวิธีการติดตั้ง Node.js บนระบบ
ติดตั้ง Node.js ด้วยวิธีที่เหมาะกับระบบ:
bash
# ติดตั้งผ่าน Scoop
scoop install nodejs
# ติดตั้งผ่าน Mise
mise use node@latest
# ติดตั้งผ่าน Proto
proto install node
bash
# ติดตั้งผ่าน Homebrew
brew install node
bash
# สำหรับ Ubuntu/Debian
sudo apt install nodejs npm
ตรวจสอบการติดตั้ง
bash
node --version
npm --version
bash
yarn --version
bash
pnpm --version
bash
bun --version
Get Started
- สร้างโปรเจคใหม่
bash
npm init
bash
yarn init -y
bash
pnpm init -y
bash
bun init -y
- รันโปรแกรม
bash
node index.js
- ติดตั้งแพ็กเกจตัวอย่าง
bash
npm install express
bash
yarn add express
bash
pnpm add express
bash
bun add express
Basic Usage
ตัวอย่างการใช้งานพื้นฐานของ Node.js:
วัตถุประสงค์ | คำอธิบาย | Modules Used |
---|---|---|
HTTP Server | สำหรับสร้างเว็บเซิร์ฟเวอร์พื้นฐาน | http |
File Reading | การอ่านเนื้อหาจากไฟล์ | fs |
File Writing | การเขียนเนื้อหาลงในไฟล์ใหม่ | fs |
Modules | การสร้างและใช้งานโมดูลแบบ custom | module.exports , require |
js
// Import http module
const http = require('http');
// Create HTTP server
const server = http.createServer((req, res) => {
// Set response header
res.writeHead(200, {'Content-Type': 'text/plain'});
// Send response
res.end('Hello from Node.js');
});
// Start server on port 3000
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
js
// Import fs module
const fs = require('fs');
// Read file asynchronously with callback
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) throw err; // Handle error
console.log(data); // Output file content
});
js
// Import fs promises API
const fs = require('fs').promises;
// Async function to read file
async function readFile() {
try {
// Await file reading
const data = await fs.readFile('file.txt', 'utf8');
console.log(data); // Output file content
} catch (err) {
console.error(err); // Handle error
}
}
js
// Import fs module
const fs = require('fs');
// Write file asynchronously
fs.writeFile('newfile.txt', 'New content', (err) => {
if (err) throw err; // Handle error
console.log('File written successfully');
});
js
// Export math functions
module.exports = {
add: (a, b) => a + b, // Addition
subtract: (a, b) => a - b // Subtraction
};
js
// Import custom math module
const math = require('./math');
// Use imported functions
console.log(math.add(5, 3)); // Output: 8
- สร้าง HTTP Server
- อ่านไฟล์
- เขียนไฟล์
- สร้างและใช้งานโมดูล
How Node.js Works
Component | Role | Description |
---|---|---|
Core Processor | ตัวประมวลผลหลักแบบ single-thread ที่จัดการการทำงานแบบไม่บล็อก | |
I/O Handler | ไลบรารีที่จัดการการทำงาน I/O แบบ asynchronous | |
JS Runtime | ตัวรัน JavaScript | |
Task Offloader | จัดการงานที่ใช้ CPU หนักในเธรดแยก |
ขั้นตอน | กระบวนการทำงาน |
---|---|
1 | รับคำขอจาก Client |
2 | มอบหมายงาน I/O ให้ Libuv จัดการ |
3 | ส่งงานที่ใช้ CPU หนักไปให้ Worker Pool |
4 | ประมวลผลงานที่เสร็จสิ้นผ่าน Event Loop |
5 | ส่งผลลัพธ์กลับผ่าน V8 |
Built-in Modules
File System & Path
โมดูลสำหรับทำงานกับระบบไฟล์และเส้นทาง
Module | Description | Common Methods | Use Case | Better Libraries |
---|---|---|---|---|
fs | ระบบไฟล์ | readFile, writeFile | อ่าน/เขียนไฟล์ | fs-extra |
path | จัดการเส้นทางไฟล์ | join, resolve | รวมเส้นทางไฟล์ | pathe |
zlib | บีบอัดข้อมูล | createGzip | บีบอัดไฟล์ | h3 |
Networking & Web
โมดูลสำหรับทำงานกับเครือข่ายและเว็บ
Module | Description | Common Methods | Use Case | Better Libraries |
---|---|---|---|---|
http | เซิร์ฟเวอร์ HTTP | createServer | สร้าง web server | h3 , nitro |
url | จัดการ URL | parse | แยกวิเคราะห์ URL | ufo |
querystring | จัดการ query string | parse | อ่าน query parameters | qs |
System & Utilities
โมดูลสำหรับทำงานกับระบบและอรรถประโยชน์
Module | Description | Common Methods | Use Case | Better Libraries |
---|---|---|---|---|
os | ข้อมูลระบบปฏิบัติการ | platform, freemem | ตรวจสอบทรัพยากรระบบ | node:os |
util | อรรถประโยชน์ | promisify | แปลง callback function | node:util |
child_process | โปรเซสย่อย | exec | รันคำสั่งระบบ | execa |
Core Functionality
โมดูลหลักสำหรับการทำงานพื้นฐาน
Module | Description | Common Methods | Use Case | Better Libraries |
---|---|---|---|---|
events | ระบบเหตุการณ์ | on, emit | สร้าง custom event system | eventemitter3 |
stream | จัดการสตรีม | createReadStream | อ่านไฟล์ขนาดใหญ่ | node:stream |
crypto | การเข้ารหัส | createHash | เข้ารหัส password | @node-rs/bcrypt |