Giải mã và mã hóa là một trong những cách thường được sử dụng để tránh các cuộc tấn công tập lệnh trên nhiều trang web [XSS] bằng cách mã hóa các ký tự đặc biệt trong một URL. Bài viết này cung cấp cách phát triển khai cũng như giải thích rõ ràng hơn. Sử dụng encode
chuyển đổi một chuỗi thành một định dạng URL hợp lệ để làm cho dữ liệu được truyền đi đáng tin cậy và an toàn hơn. Và sử dụng decode
ngược lại với quá trình mã hóa. Nó chuyển đổi các chuỗi URL đã được mã hóa và các tham số truy vấn trở lại định dạng bình thường của chúng
Trong bài viết này, bạn sẽ học cách encode
or decode
String URL and paramter query
in application Node. js
Xem thêm. mẹo viết code javascript và nodejs
mã hóa URL
Trong Node. js được xây dựng trên công cụ JavaScript V8 của Chrome Hỗ trợ hai phương pháp encode
đó là
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 để mã hóa một URL. Vì vậy nó được sử dụng như thế nào?javascript mã hóaURI
Nếu bạn muốn mã hóa một URL hoàn chỉnh thì có thể sử dụng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1, phương thức này không hỗ trợ các ký tự sau. const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4. Ví dụ sau để thấy điều đóconst url = '//example.com/!Learn Node$/ Example';
// Encode complete URL
const encodedUrl = encodeURI[url];
// Print encoded URL
console.log[encodedUrl];
// //example.com/!Learn%20Node$/%20Example
mã hóaURIThành phần
Nếu như dùng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 để mã hóa một URL thì việc sử dụng const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 thì chỉ mã hóa const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 hay còn gọi là tham số chuỗi truy vấn chứ không phải URL hoàn chỉnh. Xem tiếp ví dụ sauconst baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
Ngoài phương pháp
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 thì chúng tôi cung cấp thêm cho bạn một mô-đun tương tự nhưng nó được tích hợp sẵn trong Node. js to encode URL that is const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9mô-đun chuỗi truy vấn
Bạn cũng có thể sử dụng
const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9 được tích hợp nút. js to code URL. Mô-đun này cung cấp các phương thức tiện ích để phân tích cú pháp và định dạng chuỗi truy vấn URLconst querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
giải mã URL
Trên đó là công việc hướng dẫn cũng như giải thích về công việc mã hóa
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1. Đương nhiên, khi mã hóa thì phải giải mã, tương tự khi mã hóa xong thì phải giải mãTương tự thôi
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 cũng cung cấp cho hai phương pháp đó là const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
3 và const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 để thực hiện giải mã URLjavascript giải mãURI
Phương pháp
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
3 được sử dụng để giải mã đầy đủ một URLconst encodedUrl = '//example.com/!Learn%20Node$/%20Example';
// Decode URL
const url = decodeURI[encodedUrl];
// Print URL
console.log[url];
// //example.com/!Learn Node$/ Example
giải mãURIThành phần
Chức năng
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 được sử dụng để giải mã URL thành phầntry {
var a = decodeURIComponent['%E0%A4%A'];
} catch[e] {
console.error[e];
}
// URIError: malformed URI sequence
Lời kết
Bài viết đã rõ ràng rõ ràng cho việc sử dụng
const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 rồi, cho nên công việc tóm tắt là công việc dư thừa. Nhưng các bạn cũng nên để ý đến những công việc khác nhau giữa const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 và const baseUrl = '//example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';
// Encode query string
const encodedQuery = encodeURIComponent[query];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2, cũng như const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
3 và const querystring = require['querystring'];
const baseUrl = '//example.com/search?';
const query = 'SELECT * from users WHERE id = 1'
// Encode query string
const encodedQuery = querystring.stringify[{ q: query }];
// Build full URL
const url = baseUrl + encodedQuery;
// Print full URL
console.log[url];
// //example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4 trong Node. js. Asmising to use invalid target of the work of mình