Created
August 19, 2017 16:09
-
-
Save HassanAlgoz/65a085d32d401aefaaab43d84b068a20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http') | |
// The `url` module splits up a web address into readable parts | |
const url = require('url') | |
http.createServer((req, res) => { | |
res.writeHead(200, {'Content-Type': 'text/html'}) | |
// url.parse() will return a URL object with each part of the address as properties | |
let urlObject = url.parse(req.url, true) | |
let {query, search, pathname} = urlObject | |
console.log(pathname) | |
console.log(search) | |
console.log(query) // query object | |
let txt = query.year + " " + query.month | |
res.end(txt) | |
}).listen(8080) | |
// `url.parse(req.url, true)` will return a URL object with each part of the address as properties | |
// Below is a breakdown of the following url: | |
// https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash1 | |
// ┌─────────────────────────────────────────────────────────────────────────────────────────────┐ | |
// │ href │ | |
// ├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤ | |
// │ protocol │ │ auth │ host │ path │ hash │ | |
// │ │ │ ├──────────────┬──────┼──────────┬────────────────┤ │ | |
// │ │ │ │ hostname │ port │ pathname │ search │ │ | |
// │ │ │ │ │ │ ├─┬──────────────┤ │ | |
// │ │ │ │ │ │ │ │ query │ │ | |
// " https: // user : pass @ sub.host.com : 8080 /p/a/t/h ? query=string #hash " | |
// │ │ │ │ │ hostname │ port │ │ │ │ | |
// │ │ │ │ ├──────────────┴──────┤ │ │ │ | |
// │ protocol │ │ username │ password │ host │ │ │ │ | |
// ├──────────┴──┼──────────┴──────────┼─────────────────────┤ │ │ │ | |
// │ origin │ │ origin │ pathname │ search │ hash │ | |
// ├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤ | |
// │ href │ | |
// └─────────────────────────────────────────────────────────────────────────────────────────────┘ | |
// (all spaces in the "" line should be ignored -- they are purely for formatting) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx pro