Hướng dẫn javascript stats - thống kê javascript

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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: 1592665807796265
    
    6
    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    7
    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    8

    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: 1592665807796265
    
    9
    Using stat synchronous: 1592665807796265
    
    0
    Using stat synchronous: 1592665807796265
    
    1
    Using stat synchronous: 1592665807796265
    
    2
    Using stat synchronous: 1592665807796265
    
    3

    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
    
    3

    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
    
    4
    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
    
    5
    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
    
    6

    Using stat synchronous: 1592665807796265
    
    4
    Using stat synchronous: 1592665807796265
    
    5
    Using stat synchronous: 1592665807796265
    
    6
    Using stat synchronous: 1592665807796265
    
    7
    Using stat synchronous: 1592665807796265
    
    8

    Using stat synchronous: 1592665807796265
    
    4
    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
    
    0
    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
    
    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
    
    2

    Using stat synchronous: 1592665807796265
    
    4
    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
    
    8
    Using stat synchronous: 1592665807796265
    
    2
    Using stat synchronous: 1592665807796265
    
    3

    Using stat synchronous: 1592665807796265
    
    4
    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
    
    3

    Output:

    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    

    const Log = require('./log.js')
    
    Log.info('The first log')
    
    1
    Using stat synchronous: 1592665807796265
    
    5
    Using stat synchronous: 1592665807796265
    
    6
    Using stat synchronous: 1592665807796265
    
    7
    Using stat synchronous: 1592665807796265
    
    8

    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    6
    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    7
    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 http://${hostname}:${port}/`);
    });
    
    
    4

    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 http://${hostname}:${port}/`);
    });
    
    
    5

    Using stat synchronous: 1592665807796265
    
    4
    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 http://${hostname}:${port}/`);
    });
    
    
    7

    const Log = require('./log.js')
    
    Log.info('The first 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
    
    0
    const 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
    
    2

    Using stat synchronous: 1592665807796265
    
    4
    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
    
    0
    Viblo
    
    5

    Viblo
    
    6
    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
    
    2

    Viblo
    
    8
    Viblo
    
    9
    const fs = require('fs');
    
    fs.readFile('./test.txt', function (err, data) {
        if (err) throw err;
        console.log(data.toString());
    });
    
    console.log(1);
    
    0

    Output:

    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.

    const Log = require('./log.js')
    
    Log.info('The first 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
    
    5
    Using stat synchronous: 1592665807796265
    
    1
    Using stat synchronous: 1592665807796265
    
    2
    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
    
    3
    https://nodejs.org/api/fs.html#fs_stats_ctimens


    Mở đầu

    Với bài trước (https://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.

    Hướng dẫn javascript stats - thống kê javascript

    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 http://${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: 1592665807796265
    
    0

    Kết quả:

    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    1

    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ả:
    :
    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: 1592665807796265
    
    2

    Kết quả:

    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    1

    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ả:

    Ghi File

    Using stat: 1592665604516105.7
    Using lstat: 1592665807796265
    
    4

    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.

    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: 1592665807796265
    
    5

    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é.

    Nếu chúng ta muốn ghi thêm vào FIle chúng ta sẽ sử dụng method: appendFileQuan trọng: 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 đó.
    Xóa File:Để xóa một file với File System modules. Chúng ta sẽ sử dụng
    1
    Viblo
    
    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:

    FlagMô tả
    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é: https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback https://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é
    Hướng dẫn javascript stats - thống kê javascript

    Tham khảo: https://www.tutorialsteacher.com/nodejs/nodejs-file-system

    https://nodejs.org/docs/latest-v15.x/api/