Ta có thể dùng như sau:
const url = require('url');
Khi tạo server:
const server = http.createServer(function (req, res) {
let urlParse = url.parse(req.url, true);
res.write(`${urlParse.query.name}`);
res.end();
});
Dùng để parse url. Nếu tham số thứ 2 là false thì query sẽ chỉ là string thông thường. Nếu là true thì sẽ là dạng object.
url.parse(url, slashesDenoteHost);
// false query: 'animal=Dog&color=red'
// true { animal: 'Dog', color: 'red' }