Computer >> Máy Tính >  >> Lập trình >> Javascript

urlObject.auth () Phương thức trong Node.js

Thuộc tính auth () xác định phần tên người dùng và mật khẩu của bất kỳ URL nào, còn được gọi là userInfo. Chuỗi và tên người dùng được phân tách bằng dấu hai chấm (:).

Cú pháp

urlOject.auth()

Tham số

Vì nó chỉ lấy lại tên người dùng và mật khẩu từ một URL nên nó không yêu cầu bất kỳ thông số đầu vào nào.

Ví dụ

Tạo một tệp có tên - auth.js và sao chép đoạn mã bên dưới. Sau khi tạo tệp, sử dụng lệnh sau để chạy mã này như được hiển thị trong ví dụ bên dưới -

node auth.js

auth.js

// Importing the URL module
const url = require('url');

var adr = 'https://username=hello:password=tutorialspoint@www.tutorialspoint.com/';

// Parsing the above URL address
var q = url.parse(adr, true);

// Printing the auth details
console.log(q.auth);

Đầu ra

C:\home\node>> node auth.js
username=hello:password=tutorialspoint

Ví dụ

Hãy xem thêm một ví dụ.

// Importing the URL module
const url = require('url');

var adr = 'https://username=hello:password=tutorialspoint@www.tutorialspoint.com/';

// Parsing the above URL address
var q = url.parse(adr, true);

// Printing the auth details
console.log(q.auth);

Đầu ra

C:\home\node>> node auth.js
username=admin:password=tutorialspoint123