[Web] Node.js - URL
URL : http://exampleurl.com:126/main?id=HTML&page=3
protocol : which way of internet to follow [http : HyperText Transfer Protocol]
host : Computer which is connected to internet
port : Web basically use port number '80' as default value
path : means which directory's which file of the computer
query string : passes informations such as what is which page to show, always starts with '?' and value is emphasized
(?id=HTML&page=3 means "id is HTML and page is 3")
By passed requests of client to query string, the server responses.
And developers will make that progress.
var http = require('http');
var fs = require('fs');
var url = require('url');
//create server
var app = http.createServer(function(request,response){
var _url = request.url;
//parsing directory/file name after 'url.'
//var pathname = url.parse(request.url).pathname;
var queryData = url.parse(_url, true).query;
var title = queryData.id;
if(_url == '/'){
title = 'Welcome';
}
if(_url == '/favicon.ico'){
return response.writeHead(404);
}
response.writeHead(200);
var template = `
<!doctype html>
<html>
<head>
<title>${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">Web</a></h1>
<ol>
<li><a href="/?id=list1">list1</a></li>
<li><a href="/?id=list2">list2</a></li>
<li><a href="/?id=list3">list3</a></li>
</ol>
<h2>${title}</h2>
<p>Hypertext Markup Language (HTML) is the standard markup language for
<strong> creating <u>web</u> pages </strong>and web applications.<br>
Web browsers receive HTML documents from a web server or from local storage and render them into multimedia web pages.<br>
HTML describes the structure of a web page semantically and originally included cues for the appearance of the document.
</p>
<p style="margin-top:45px;">HTML elements are the building blocks of HTML pages.<br>
With HTML constructs, images and other objects,
such as interactive forms, may be embedded into the rendered page.<br>
It provides a means to create structured documents by denoting structural semantics for text
such as headings, paragraphs, lists, links, quotes and other items.<br>
HTML elements are delineated by tags, written using angle brackets.
</p>
</body>
</html>
`;
response.end(template);
});
app.listen(3000);
require() : Node.js use this function to call module.
[line1,2,3]
what is module?
Parsing
[line10]
// urlExmaple.js
var url = require('url');
var parsedObject = url.parse('http://user:pass@host.com:8080/p/a/t/h?query=string#hash');
console.log(parsedObject); // print information of url object
console.log(url.format(parsedObject)); // print url object to string
Result
$ node urlExample.js
{ protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:8080',
port: '8080',
hostname: 'host.com',
hash: '#hash',
search: '?query=string',
query: 'query=string',
pathname: '/p/a/t/h',
path: '/p/a/t/h?query=string',
href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash' }
http://user:pass@host.com:8080/p/a/t/h?query=string#hash
Reference List
English : https://millermedeiros.github.io/mdoc/examples/node_api/doc/url.html