peri hokiperihokiclarustologisticsrimashaop.comangelpaintingtools.co.invrjanaenergies.comgates of olympus perihoki jadi mesin uang paling gacortrik main baccarat perihoki ala pro bikin pemula jadi jutawanpantang pulang sebelum jackpot main dadu sicbo perihokipgsoft mahjong ways 2 muncul scatter merah tiap spin perihokicara menciptakan akun perihokigacor mahjong wins 3jackpot kembar mahjong wins 3 abc1131cara menang stabil rtp mahjong ways abc1131bu nurlina menang 197 juta mahjong ways 2mahasiswa cuan 421 juta karena pola mahjongpemilik warung menang 128 juta mahjong wins 3strategi bravy erika mahjong abc1131 terungkappemuda jambi taklukkan mahjong wins 3penjual jamu menang besar mahjong wins 5pemuda kampung raup 180 juta mahjong wayskisah inspiratif petani mahjong ways keberhasilanmahasiswa menang 92 juta mahjong waysmodal warung kopi bang ojak sukses mahjong waystanpa modal maxwin mahjong wins 3 strategibocoran mahjong ways spin kombinasicara bermain mahjong untuk meraih keberuntungankeajaiban mahjong kasir indomaret sukses beli tanahmeraup keuntungan maksimal di mahjong winsid rahasia scatter hitamid pola scatterid jackpot mahjong waysid spin mahjong winsid tips jitu mahjongrahasia kemenangan mahjong ways spin slow

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Giới thiệu module Yargs trong NodeJS - Freetuts
CĂN BẢN
SERVER
MYSQL
MONGODB
MODULE
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Giới thiệu module Yargs trong NodeJS

Trong bài này mình sẽ tìm hiểu đến một module rất hữu ích trong việc lấy dữ liệu từ người dùng đó là module yargs, công cụng của nó giống như việc lấy dữ liệu với phương thức GET trong PHP vậy.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Yargs là gì?

Yargs là một module có tác dụng tách chuỗi của các request và lấy các tham số + giá trị của các tham số, module này rất hữu ích trong việc giao tiếp lấy dữ liệu từ client.

Cài đặt yargs:

Để cài đặt yargs thì bạn bật Command Prompt lên và gõ dòng lệnh sau rồi nhấn Enter:

Bài viết này được đăng tại [free tuts .net]

npm install yargs

Sau đó bạn chờ nó cài đặt Version mới nhất của Yargs.

2. Cách sử dụng Yargs căn bản

Tạm thời các bạn cứ hiểu rằng URL chính là cú pháp của command line mà ta gõ để chạy file js, vì vậy để truyền các tham số thì ta sẽ bổ sung trong command line luôn.

Trước tiên bạn tạo một file tên là yargs-demo.js nằm trong thư mục D:/nodejs-freetuts, chúng ta sẽ thực hành trên file này.

Truyền tham số dạng chỉ có value.

Tất cả các tham số sẽ được lưu trữ trong thuộc tính argv của Yargs. Bạn mở file yargs-demo.js và nhập vào nội dung sau:

var yargs = require('yargs');

// Lấy hết tham số
var argv = yargs.argv;

// In ra xem thử
console.log(argv);

Và chạy với đoạn code sau:

node yargs-demo.js

Kết quả trả về như hình sau:

yargs can ban png

Tát cả các tham số sẽ được lưu trong key _ của đối tượng trả về argv.

Ok tiếp theo ta thử chạy với đoạn code sau:

node yargs-demo.js cuong freetuts

Kết quả nó sẽ như hình sau:

yargs can ban 1 png

Như vậy nếu ta truyền các giá trị đằng sau thì nó sẽ lưu vào key _ và các tham số đó sẽ nằm trong một mảng. Không tin bạn thay đổi lại file yargs-demo.js như sau:

var yargs = require('yargs');

// Lấy hết tham số
var argv = yargs.argv._;

// In ra xem thử
console.log(argv);

Thì kết quả nó trả về một mảng:

yargs can ban 2 png

Truyền tham số dạng key => value.

Để truyền tham sô dạng key => value thì ta sử dụng cú pháp sau:

node yargs-demo.js --key=value
hoặc
node yargs-demo.js --key=value

Ví dụ: Truyền tham số domain và author

Bạn chỉnh lại file yargs-demo.js như sau:

var yargs = require('yargs');

// Lấy hết tham số
var argv = yargs.argv;

// In ra xem thử
console.log(argv);

Chạy lên với mã code:

node yargs-demo.js --domain freetuts.net --author thehalfheart

Thì kết quả như hình sau:

yargs can ban 3 png

Vậy nếu ta dùng cú pháp --key value thì nó sẽ không lưu vào key _ nữa mà sẽ tạo một key riêng luôn.

3. Ví dụ sử dụng Yargs căn bản

Bây giờ ta làm một ví dụ như sau: Viết chương trình kiểm tra số n từ người dùng nhập vào có phải là số nguyên tố hay không.

Để kiểm tra số nguyên tố thì mình sẽ dùng một hàm trong bài kiểm tra số nguyên tố bằng javascript nhé.

// Hàm kiểm tra số nguyên tố
function kiem_tra_snt(n)
{
    // Biến cờ hiệu
    var flag = true;
 
    // Nếu n bé hơn 2 tức là không phải số nguyên tố
    if (n < 2){
        flag = false;
    }
    else if (n == 2){
        flag = true;
    }
    else if (n % 2 == 0){
      flag = false;
    }
    else{
        // lặp từ 3 tới n-1 với bước nhảy là 2 (i+=2)
        for (var i = 3; i < n-1; i+=2)
        {
            if (n % i == 0){
                flag = false;
                break;
            }
        }
    }
 
    return flag;
}


// Main Code
var yargs = require('yargs');

var argv = yargs.argv;

if (typeof argv.n == "undefined"){
    console.log('Ban chua nhap n');
}
else{
    if (kiem_tra_snt(argv.n)){
        console.log('La so nguyen to');
    }
    else{
        console.log('Khong phai so nguyen to');
    }
}

Bây giờ bạn chạy với lệnh:

node yargs-demo.js

Thì nó sẽ thông báo là chưa nhập n. Bạn thử chạy với lệnh sau:

node yargs-demo.js --n 11

Thì nó sẽ thông báo là số nguyên tố.

4. Lời kết

Trên là một số ví dụ rất basic về yargs, vẫn còn khá nhiều vấn đề nữa đó là tùy chọn options trong Yargs nhưng mình sẽ trình bày ở một video khác nhé. Bài này mình dừng ở đây, hẹn gặp lại bạn ở bài tiếp theo.

Cùng chuyên mục:

Cách gửi mail trong NodeJS có file đính kèm

Cách gửi mail trong NodeJS có file đính kèm

Crawl dữ liệu website bằng NodeJS cơ bản

Crawl dữ liệu website bằng NodeJS cơ bản

CORS là gì ? Xử lý CORS trong NodeJS

CORS là gì ? Xử lý CORS trong NodeJS

Chắc hẳn trong quá trình xử lý các request tới server thì một vài trường…

Xử lý form trong Express

Xử lý form trong Express

Xây dựng URL trong Express

Xây dựng URL trong Express

Trong bài viết này chúng ta sẽ cùng nhau đi tìm hiểu về cách xây…

Populate trong Mongoose

Populate trong Mongoose

Error Handling & Debugging trong Socket.io

Error Handling & Debugging trong Socket.io

Namespaces & Rooms trong Socket.io

Namespaces & Rooms trong Socket.io

Middleware trong Mongoose

Middleware trong Mongoose

Create / Read / Update / Delete trong Mongoose

Create / Read / Update / Delete trong Mongoose

Validation trong Mongoose

Validation trong Mongoose

Subdocuments trong Mongooose

Subdocuments trong Mongooose

Documents trong Mongoose

Documents trong Mongoose

Trong bài viết này chúng ta sẽ tìm hiểu về documents trong Mongoose

Models trong Mongoose

Models trong Mongoose

SchemaTypes trong Mongoose

SchemaTypes trong Mongoose

Schemas trong Mongoose

Schemas trong Mongoose

Mongoose trong NodeJS

Mongoose trong NodeJS

Làm việc với Table MySQL sử dụng Nodejs

Làm việc với Table MySQL sử dụng Nodejs

Kết nối với MongoDB trong NodeJS

Kết nối với MongoDB trong NodeJS

Giới thiệu MongoDB trong NodeJS

Giới thiệu MongoDB trong NodeJS

Top