Node grgtrnhtshsrt
เมื่อเรา Node Built-in Modules เราจะ import modules from node:modules
js
// import fs จาก node:fs
import fs from 'node:fs';
// การอ่านไฟล์
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
console.log('File contents:', data);
// การเขียนไฟล์หลังจากอ่านเสร็จ
fs.writeFile('example.txt', 'Hello, Node.js ESM!', (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
console.log('File written successfully.');
// การเช็คว่ามีไฟล์หรือไม่
fs.access('example.txt', fs.constants.F_OK, (err) => {
if (err) {
console.log('File does not exist.');
} else {
console.log('File exists.');
}
// การลบไฟล์หลังจากเช็ค
fs.unlink('example.txt', (err) => {
if (err) {
console.error('Error deleting file:', err);
return;
}
console.log('File deleted successfully.');
});
});
});
});
Error Handling and Inspection
Module | Description |
---|---|
assert | ให้ฟังก์ชันสำหรับการตรวจสอบ. |
domain | ให้วิธีการจัดการข้อผิดพลาดใน asynchronous. |
Data and File Handling
Module | Description |
---|---|
buffer | ให้คลาสสำหรับจัดการข้อมูลไบนารี. |
fs | ให้ฟังก์ชันการทำงานเกี่ยวกับระบบไฟล์. |
path | ให้ยูทิลิตี้สำหรับการทำงานกับเส้นทางไฟล์และไดเร็กทอรี. |
string_decoder | ให้วิธีการถอดรหัสสตรีมของสตริง. |
Process and Networking
Module | Description |
---|---|
child_process | อนุญาตให้คุณสร้างและติดต่อกับ child processes. |
cluster | อำนวยความสะดวกในการสร้าง cluster ของ Node.js processes. |
net | ให้ฟังก์ชันการทำงานเกี่ยวกับเครือข่าย. |
dgram | ใช้สำหรับ UDP datagram sockets. |
http | ให้ฟังก์ชันการทำงานของ HTTP server และ client. |
https | ให้ฟังก์ชันการทำงานของ HTTPS server และ client. |
Process Management
Module | Description |
---|---|
process | ให้ข้อมูลและควบคุมโปรเซสปัจจุบันของ Node.js. |
timers | ให้ฟังก์ชันสำหรับการจัดตารางเวลาและการจัดการ timers. |
Stream Handling
Module | Description |
---|---|
stream | ให้ยูทิลิตี้สำหรับการทำงานกับสตรีม. |
readline | ให้อินเตอร์เฟซสำหรับอ่านข้อมูลจาก readable stream. |
Text and URL Handling
Module | Description |
---|---|
querystring | ให้ยูทิลิตี้สำหรับการแยกวิเคราะห์และจัดรูปแบบ URL query strings. |
url | ให้ยูทิลิตี้สำหรับการแก้ไขและแยกวิเคราะห์ URL. |
Debugging and Output
Module | Description |
---|---|
console | ให้ console debugging ที่ง่าย. |
inspector | ให้ API สำหรับการดีบักแอปพลิเคชัน Node.js. |
repl | ให้ Read-Eval-Print Loop (REPL) สำหรับการเขียนโปรแกรมเชิงโต้ตอบ. |
Code Management and Execution
Module | Description |
---|---|
module | จัดการการโหลดและการจัดการ modules. |
vm | ให้ API สำหรับการคอมไพล์และรันโค้ดใน VM (Virtual Machine). |
worker_threads | ให้ฟังก์ชันการทำงานกับ threads. |
Security and Cryptography
Module | Description |
---|---|
crypto | ให้ฟังก์ชันการเข้ารหัสและการเข้ารหัสลับ. |
tls | ใช้สำหรับการสื่อสารเครือข่ายที่ปลอดภัยโดยใช้โปรโตคอล TLS/SSL. |
Performance and Profiling
Module | Description |
---|---|
trace_events | ให้ API สำหรับการติดตามและการวิเคราะห์เหตุการณ์. |
v8 | ให้ API สำหรับการโต้ตอบกับเครื่องยนต์ JavaScript V8. |
TTY and Terminal
Module | Description |
---|---|
tty | ให้คลาสสำหรับการทำงานกับ TTY (teletypewriter) devices. |
High-Performance Data Handling
Module | Description |
---|---|
zlib | ให้ฟังก์ชันการบีบอัดและการคลายการบีบอัดข้อมูล. |