Skip to content

Instantly share code, notes, and snippets.

@nguyentien98
Last active November 1, 2018 04:00
Show Gist options
  • Save nguyentien98/8b38cb0ad65adb3de8cedd5f1c9f85b0 to your computer and use it in GitHub Desktop.
Save nguyentien98/8b38cb0ad65adb3de8cedd5f1c9f85b0 to your computer and use it in GitHub Desktop.

URL module docs

URL

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();
});

Hàm parse

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' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment