Node.js như một máy chủ tệp
Mô -đun hệ thống tệp Node.js cho phép bạn làm việc với hệ thống tệp trên máy tính của bạn.
Để bao gồm mô -đun hệ thống tệp, hãy sử dụng phương thức
require[]
:
Sử dụng phổ biến cho mô -đun hệ thống tệp:
- Đọc tệp
- Tạo tập tin
- Cập nhật tệp
- Xóa các tập tin
- Đổi tên tập tin
Đọc tệp
Phương thức fs.readFile[]
được sử dụng để đọc các tệp trên máy tính của bạn.
Giả sử chúng ta có tệp HTML sau [nằm trong cùng thư mục với Node.js]:
demofile1.html
Đoạn đầu của tôi.
My Header
My paragraph.
Tạo tệp Node.js đọc tệp HTML và trả về nội dung:
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
var fs = require['fs'];
http.createServer[function [req, res] {
fs.readFile['demofile1.html', function[err, data] {
res.writeHead[200, {'Content-Type': 'text/html'}];
res.write[data];
return res.end[];
}];
}].listen[8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Bắt đầu demo_readfile.js:
C: \ user \ tên của bạn> node demo_readfile.js
Nếu bạn đã làm theo cùng các bước trên máy tính của mình, bạn sẽ thấy kết quả tương tự như ví dụ: // localhost: 8080
Tạo tập tin
Mô -đun hệ thống tệp có các phương thức tạo tệp mới:
fs.appendFile[]
fs.open[]
fs.writeFile[]
Phương thức fs.appendFile[]
nối nội dung được chỉ định vào một tệp. Nếu tệp không tồn tại, tệp sẽ được tạo:
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['Saved!'];
}];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['Saved!'];
}];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['Saved!'];
}];
Bắt đầu demo_readfile.js:
C: \ user \ tên của bạn> node demo_readfile.js
Nếu bạn đã làm theo cùng các bước trên máy tính của mình, bạn sẽ thấy kết quả tương tự như ví dụ: // localhost: 8080
fs.appendFile[]
fs.writeFile[]
Tạo tập tin
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['Updated!'];
}];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['Replaced!'];
}];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Bắt đầu demo_readfile.js:
C: \ user \ tên của bạn> node demo_readfile.js
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['File deleted!'];
}];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Bắt đầu demo_readfile.js:
C: \ user \ tên của bạn> node demo_readfile.js
Thí dụ
var http = yêu cầu ['http']; var fs = yêu cầu ['fs']; http.createserver [function [req, res] {& nbsp; fs.readfile ['demofile1.html', function [err, data] & NBSP; []; & nbsp;}];}]. Nghe [8080];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
if [err] throw err;
console.log['File Renamed!'];
}];
Chạy ví dụ »
Lưu mã ở trên trong một tệp có tên là "demo_readfile.js" và bắt đầu tệp:
Bắt đầu demo_readfile.js:
C: \ user \ tên của bạn> node demo_readfile.js