Skip to content

Instantly share code, notes, and snippets.

@HassanAlgoz
Created August 19, 2017 16:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HassanAlgoz/65a085d32d401aefaaab43d84b068a20 to your computer and use it in GitHub Desktop.
Save HassanAlgoz/65a085d32d401aefaaab43d84b068a20 to your computer and use it in GitHub Desktop.
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)
@mohamedo7x
Copy link

thx pro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment