Hướng dẫn how do i create a node js html file? - làm cách nào để tạo một tệp html nút js?


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ụ: http: // 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ụ: http: // 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



Làm cách nào để tạo tệp HTML nút?

Phương thức 2: Tạo tệp chỉ var fs = yêu cầu ('fs');var filename = 'path/to/file';var stream = fs.createwriteStream (tên tệp);dòng.một lần ('mở', hàm (fd) {var html = buildHtml (); stream. end (html);});var fs = require('fs'); var fileName = 'path/to/file'; var stream = fs. createWriteStream(fileName); stream. once('open', function(fd) { var html = buildHtml(); stream. end(html); });

Làm cách nào để viết một tệp Node JS?

Phương thức fs.WriteFile () được sử dụng để ghi không đồng bộ dữ liệu được chỉ định vào một tệp.Theo mặc định, tệp sẽ được thay thế nếu nó tồn tại.Tham số 'Tùy chọn' có thể được sử dụng để sửa đổi chức năng của phương thức.fs. writeFile() method is used to asynchronously write the specified data to a file. By default, the file would be replaced if it exists. The 'options' parameter can be used to modify the functionality of the method.

Làm cách nào để hiển thị tệp HTML nút JS?

Phương thức Res.SendFile () của mô -đun Express.js được sử dụng để hiển thị một tệp HTML cụ thể có trong máy cục bộ. sendFile() method of the express. js module is used to render a particular HTML file that is present in the local machine.