Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcstats.ctimeNs property is an inbuilt application programming interface of the fs.Stats class is used to get the timestamp when the file status has been changed last time since the POSIX epoch expressed in nanoseconds.
Syntax:
stats.ctimeNs;
Bàn luận Properties does not have any parameter, but during creation of stats object {bigint:true} must be passed as options.
Thuộc tính STATS.CTIMENS là giao diện lập trình ứng dụng sẵn có của lớp Fs.Stats được sử dụng để có dấu thời gian khi trạng thái tệp đã được thay đổi lần trước kể từ khi Epoch POSIX thể hiện bằng nano giây. It returns a number or BigInt value that represents the timestamp when the file status has been changed last time since the POSIX epoch expressed in nanoseconds.
Tham số: Thuộc tính không có bất kỳ tham số nào, nhưng trong quá trình tạo đối tượng thống kê {bigint: true} phải được truyền dưới dạng tùy chọn.
Giá trị trả về: Nó trả về một số hoặc giá trị Bigint đại diện cho dấu thời gian khi trạng thái tệp đã được thay đổi lần trước kể từ khi Epoch Posix thể hiện bằng nano giây.
Using stat: 1592665604516105.7 Using lstat: 15926658077962656
Using stat: 1592665604516105.7 Using lstat: 15926658077962657
Using stat: 1592665604516105.7 Using lstat: 15926658077962658
Các ví dụ dưới đây minh họa việc sử dụng thuộc tính STATS.CTIMENS trong Node.js:
Ví dụ 1:
Using stat: 1592665604516105.7 Using lstat: 15926658077962659
Using stat synchronous: 15926658077962650
Using stat synchronous: 15926658077962651
Using stat synchronous: 15926658077962652
Using stat synchronous: 15926658077962653
var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
3var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
4var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
5var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
6Using stat synchronous: 15926658077962654
Using stat synchronous: 15926658077962655
Using stat synchronous: 15926658077962656
Using stat synchronous: 15926658077962657
Using stat synchronous: 15926658077962658
Using stat synchronous: 15926658077962654
var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
0var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
1 var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
2Using stat synchronous: 15926658077962654
var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
8Using stat synchronous: 15926658077962652
Using stat synchronous: 15926658077962653
Using stat synchronous: 15926658077962654
var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
3Output:
Using stat: 1592665604516105.7 Using lstat: 1592665807796265
1const Log = require['./log.js']
Log.info['The first log']
Using stat synchronous: 1592665807796265
5 Using stat synchronous: 1592665807796265
6Using stat synchronous: 1592665807796265
7 Using stat synchronous: 1592665807796265
8
Using stat: 1592665604516105.7 Using lstat: 15926658077962656
Using stat: 1592665604516105.7 Using lstat: 15926658077962657
const http = require['http'];
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer[[req, res] => {
res.statusCode = 200;
res.setHeader['Content-Type', 'text/plain'];
res.end['Hello, World!\n'];
}];
server.listen[port, hostname, [] => {
console.log[`Server running at //${hostname}:${port}/`];
}];
4const http = require['http'];
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer[[req, res] => {
res.statusCode = 200;
res.setHeader['Content-Type', 'text/plain'];
res.end['Hello, World!\n'];
}];
server.listen[port, hostname, [] => {
console.log[`Server running at //${hostname}:${port}/`];
}];
5Using stat synchronous: 15926658077962654
const http = require['http'];
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer[[req, res] => {
res.statusCode = 200;
res.setHeader['Content-Type', 'text/plain'];
res.end['Hello, World!\n'];
}];
server.listen[port, hostname, [] => {
console.log[`Server running at //${hostname}:${port}/`];
}];
7const Log = require['./log.js']
Log.info['The first log']
1var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
0const Log = require['./log.js']
Log.info['The first log']
8 var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
2Using stat synchronous: 15926658077962654
var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
0Viblo
5Viblo
6var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
2Viblo
8Viblo
9const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
0Output:
Using stat synchronous: 1592665807796265
Ví dụ 2: The above program will compile and run by using the
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
1 command and use the file_path correctly.
1const Log = require['./log.js']
Log.info['The first log']
5var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
Using stat synchronous: 1592665807796265
1Using stat synchronous: 1592665807796265
2
3
//nodejs.org/api/fs.html#fs_stats_ctimensvar log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
Mở đầu
Với bài trước [//viblo.asia/p/nodejs-bai-1-hello-world-ORNZqn4Ll0n] mình đã giới thiệu qua và cài đặt Node.js trên các OS khác nhau cũng như đã chạy thử một server Node.js. Trong bài này chúng ta cùng đến với một số thành phần, công cụ cơ bản cụ thể là Modules trong Node.js và xử lý File với fs module nhé. Chúng ta cùng bắt đầu bài viết.fs module nhé. Chúng ta cùng bắt đầu bài viết.
1. Node.js modules
Node.js modules là gì
Module trong Node.js tuơng tự với các thư viện JavaScript. Nó là một hoặc nhiều chứng năng khác nhau được đóng gói và thêm vào ứng dụng chạy Node.js của chúng ta. Việc chia nhỏ module ra như vậy không cần phải nói thêm nhiều nữa, nó giúp chúng ta dễ dàng quản lý, bảo trì và tái sử dụng code hơn. Chúng ta cũng có thể đóng gói và đẩy lên kho thư viện NPM để mọi người có thể sử dụng nó. Ví dụ nếu chúng ta cần kết nốt tới sql hoặc MongoDB chúng ta sẽ cần tạo các module cho nó và sử dụng lại trong ứng dụng của mình.
Để tự tạo một module. Chúng ta viết chúng như những đoạn mã Javascript thông thường. Và sau đó sử dụng module.exports để khai báo với Node.js rằng chúng ta có thể sử dụng nó. Ví dụ dưới đây chúng ta có thể tạo ra một module để thực hiện việc custom console.log để dùng chung một pattern trong ứng dụng:
var log = {
info: function [info] {
console.log['Info: ' + info];
},
warning:function [warning] {
console.log['Warning: ' + warning];
},
error:function [error] {
console.log['Error: ' + error];
}
};
module.exports = log
Để sử dụng lại module trên, chúng ta dùng cú pháp
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
2 như bên dưới:const Log = require['./log.js']
Log.info['The first log']
Module http
Như bài trước mọi người cũng có thể thấy mình có sử dụng module
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
3 để tạo một web server. Mục đích của Web server là xử lý các http requests cho ứng dụng web, ví dụ như Apache là web server cho các ứng dụng web PHP hoặc Java. Chúng ta có thể sử dụng Apache để chạy một ứng dụng web Node.js tuy nhiên vẫn nên dùng Node.js web server.const http = require['http'];
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer[[req, res] => {
res.statusCode = 200;
res.setHeader['Content-Type', 'text/plain'];
res.end['Hello, World!\n'];
}];
server.listen[port, hostname, [] => {
console.log[`Server running at //${hostname}:${port}/`];
}];
Module
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
3 là core module của Node.js vì vậy chúng ta không cần cài đặt thêm từ NPM.2. Node.js FIle System
Cũng như các ngôn ngữ khác, việc xử lý file là điều không thể thiếu cho các ứng dụng. Node.js có một module giúp ta thực hiện truy cập đọc ghi các file trên hệ thống một cách dễ dàng với module
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
5Đọc File
Để đọc một file có sẵn trên hệ thống chúng ta sử dụng
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
6 của module fs.Viblo
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
Kết quả:
1
Viblo
Tại sao 1 lại được log ra trước nhỉ =]] Thực ra readFile là để xử lý việc đọc file 1 cách bất đồng bộ [asynchronously] vì vậy nó sẽ được xem như là chạy ngầm tiến trình đọc file này và thực hiện console.log[1] song song.
Vậy nếu chúng ta muốn sau khi đọc xong file mới thực hiện console.log[1] thì chúng ta cần đổi
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
7 thành const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
8 vậy là chúng ta có thể thực hiền một cách đống bộ việc đọc file với các tiến trình khác : kết quả:Viblo
1
Ghi File
Với thao tác ghi vào File chúng ta có thể ghi đè nội dung file bằng
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
9 của const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
5 module.Using stat: 1592665604516105.7 Using lstat: 15926658077962650
Kết quả:
Using stat: 1592665604516105.7 Using lstat: 15926658077962651
Tại sao 1 lại được log ra trước nhỉ =]] Thực ra readFile là để xử lý việc đọc file 1 cách bất đồng bộ [asynchronously] vì vậy nó sẽ được xem như là chạy ngầm tiến trình đọc file này và thực hiện console.log[1] song song.
Vậy nếu chúng ta muốn sau khi đọc xong file mới thực hiện console.log[1] thì chúng ta cần đổi
7 thành const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
8 vậy là chúng ta có thể thực hiền một cách đống bộ việc đọc file với các tiến trình khác : kết quả:: const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
9 sẽ ghi đè nội dung lên file cũ nên mọi người cẩn thận khi dùng nhé.Ghi File
Using stat: 1592665604516105.7 Using lstat: 15926658077962652
Kết quả:
Using stat: 1592665604516105.7 Using lstat: 15926658077962651
Tại sao 1 lại được log ra trước nhỉ =]] Thực ra readFile là để xử lý việc đọc file 1 cách bất đồng bộ [asynchronously] vì vậy nó sẽ được xem như là chạy ngầm tiến trình đọc file này và thực hiện console.log[1] song song.: Cả 2 phuơng thức trên cũng là phuơng thức để tạo 1 file mới khi trong hệ thống chúng ta chưa có file với tên đó.
Vậy nếu chúng ta muốn sau khi đọc xong file mới thực hiện console.log[1] thì chúng ta cần đổi const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
7 thành const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
8 vậy là chúng ta có thể thực hiền một cách đống bộ việc đọc file với các tiến trình khác : kết quả:
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
Ghi File
Using stat: 1592665604516105.7 Using lstat: 15926658077962654
Với thao tác ghi vào File chúng ta có thể ghi đè nội dung file bằng const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
9 của const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
5 module.
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
Tuơng tự với readFile chúng ta cũng sẽ có
1
Viblo
1 phục vụ mục đích xử lý đồng bộ.Using stat: 1592665604516105.7 Using lstat: 15926658077962655
Lưu ý:
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
9 sẽ ghi đè nội dung lên file cũ nên mọi người cẩn thận khi dùng nhé.
Xóa File: | Để xóa một file với File System modules. Chúng ta sẽ sử dụng 3 |
r+ | Mở file để đọc và ghi. Tuơng tự nếu file không tồn tại sẽ báo lỗi |
rs | Mở file để đọc file một cách đồng bộ |
rs+ | Mở file để đọc và ghi một cách đồng bộ. |
w | Mở file để ghi. File mới sẽ được tạo nếu không có sẵn và bị ghi đè nếu có sẵn |
wx | Tuơng tự như w nhưng sẽ lỗi nếu file không tồn tại |
w+ | Mở file để đọc và ghi. File mới sẽ được tạo nếu không có sẵn và bị ghi đè nếu có sẵn |
wx+ | Tuơng tự như w+ nhưng sẽ lỗi nếu file không tồn tại |
a | Mở file để chèn thêm dữ liệu. Nếu file không tồn tại sẽ tạo mới file |
ax | Tuơng tự a nhưng sẽ lỗi nếu file không tồn tại |
a+ | Mở file để đọc và chèn thêm dữ liệu. Nếu file không tồn tại sẽ tạo mới file |
ax+ | Tuơng tự a+ nhưng sẽ lỗi nếu file không tồn tại |
Một số phuơng thức quan trọng xử lý file với fs module:
fs.readFile[fileName [,options], callback] | Đọc một file có sẵn. |
fs.writeFile[filename, data[, options], callback] | Ghi đè lên file, tạo file mới nếu file không có sẵn |
fs.open[path, flags[, mode], callback] | Mở file để thực hiện đọc hoặc ghi |
fs.rename[oldPath, newPath, callback] | Thay đổi tên file |
fs.chown[path, uid, gid, callback] | Thay đổi quyền xử lý tệp , bất đồng bộ. |
fs.stat[path, callback] | Trả về đối tượng fs.Stats với những thông tin cơ bản của file |
fs.link[srcpath, dstpath, callback] | Tạo một liên kết mới từ đường dẫn hiện tại của file tới đường dẫn mới. |
fs.rmdir[path, callback] | Đổi tên một thư mục có sẵn |
fs.mkdir[path[, mode], callback] | Tạo mới một thư mục |
fs.readdir[path, callback] | Đọc nội dung của một thư mục chỉ định |
fs.utimes[path, atime, mtime, callback] | Thay đổi timestamps của file |
fs.exists[path, callback] | Kiểm tra xem file có tồn tại không |
fs.access[path[, mode], callback] | Kiểm tra quyền truy cập file . |
fs.appendFile[file, data[, options], callback] | Thêm nội dung vào file |
fs.copyFile[src, dest[, mode], callback] | Copy file tới một đường dẫn cho trước một cách bất đồng bộ |
Một số phuơng thức khác với File System mọi người vào đọc thêm ở dưới link này nhé: //nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback //nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
3. Kết bài
Với 2 phần cơ bản này chúng ta đang tiến sâu vào kiến thức của Node.js. Mình đã giới thiệu qua về modules trong Node.js, xử lý file với module
const fs = require['fs'];
fs.readFile['./test.txt', function [err, data] {
if [err] throw err;
console.log[data.toString[]];
}];
console.log[1];
5 File System. Hy vọng bài viết đem lại cách dễ dàng để tiếp cận và làm việc với Node.js cho mọi người. Cùng đón đọc những bài viết tiếp theo của mình về Node.js nhé. Cảm ơn mọi người đã theo dõi bài viết nếu thấy hay thì hãy upvote giúp mình nhé Tham khảo: //www.tutorialsteacher.com/nodejs/nodejs-file-system
//nodejs.org/docs/latest-v15.x/api/