HTTP

1. URL contains what? What is the function of each part of the URL?

URL => Uniform Resource Locator

Hosting:

Means that all the web pages and their supporting files are available on that computer. The web server will send any web page from the website it is hosting to any user’s browser, per user request.

Perfect Explanation of Webpage and Web Server:

Don’t confuse websites and web servers. For example, if you hear someone say, “My website is not responding”, it actually means that the web server is not responding and therefore the website is not available. More importantly, since a web server can host multiple websites, the term web server is never used to designate a website, as it could cause great confusion. In our previous example, if we said, “My web server is not responding”, it means that multiple websites on that web server are not available.

 Read Articles Below!!!
  1. How does the Internet work?
  2. What is the difference between webpage, website, web server, and search engine?
  3. What is a web server?
  4. What are hyperlinks? The concepts behind links on the web.
  5. What is a URL?
  6. What is the difference between URI, URL and URN?

URL Components URL Example

2. What the function of DNS? How to use nslookup command?

The domain name system resolves the names of internet sites with their underlying IP addresses adding efficiency and even security in the process.

Think of DNS like your smartphone’s contact list, which matches people’s name with their phone numbers and email addresses. Then multiply that contact list by everyone else on the planet. Example:

  • nslookup baidu.com
    • Server: 192.168.0.1 Address: 192.168.0.1#53
    • Non-authoritative answer:
      - Name:	baidu.com Address: 220.181.38.148
      
      • Name: baidu.com Address: 39.156.69.79
  • nslookup google.com
    • Server: 192.168.0.1 Address: 192.168.0.1#53
    • Non-authoritative answer:
      - Name:	google.com Address: 172.217.5.14
      
  • nslookup github.com
    • Server: 192.168.0.1 Address: 192.168.0.1#53
    • Non-authoritative answer:
      - Name:	github.com Address: 140.82.112.4
      
  • nslookup yueran-yu.github.io
    • SServer: 192.168.0.1 Address: 192.168.0.1#53
    • Non-authoritative answer:
      - Name:	yueran-yu.github.io Address: 185.199.110.153
      
      • Name: yueran-yu.github.io Address: 185.199.108.153
      • Name: yueran-yu.github.io Address: 185.199.109.153
      • Name: yueran-yu.github.io Address: 185.199.111.153

3. What the IP used for? How to use ping?

An IP address allows information to be sent and received by the correct parties, which means they can also be used to track down a user’s physical location.

  • Some special IP
    • 127.0.0.1 represent your laptop itself
    • localhost: this is a hostname that refers to the current computer used to access it.
    • 0.0.0.0 doesn’t represent any devices.

4. What is Domain, what’s the type of Domain?

A domain name is your website name. A domain name is the address where Internet users can access your website. A domain name is used for finding and identifying computers on the Internet. Computers use IP addresses, which are a series of number. However, it is difficult for humans to remember strings of numbers. Because of this, domain names were developed and used to identify entities on the Internet rather than using IP addresses.

Knowledge Points

  • A domain name can correspond to different IPs.
    • This is called load balance to prevent one machine from breaking down.
  • One IP corresponds to a different domain name.
    • This is called a Shared host, and poor developers do it.

Domain Name

HTTP Request & Response

You can read the article in MDN as well: HTTP Messages

var http = require('http')
var fs = require('fs')
var url = require('url')
var port = process.argv[2]

if (!port) {
  console.log('请指定端口号好不啦?\nnode server.js 8888 这样不会吗?')
  process.exit(1)
}
var server = http.createServer(function (request, response) {
  var parsedUrl = url.parse(request.url, true)
  var pathWithQuery = request.url
  var queryString = ''
  if (pathWithQuery.indexOf('?') >= 0) { queryString = pathWithQuery.substring(pathWithQuery.indexOf('?')) }
  var path = parsedUrl.pathname
  var query = parsedUrl.query
  var method = request.method

  /******** 从这里开始看,上面不要看 ************/

  console.log('有个傻子发请求过来啦!路径(带查询参数)为:' + pathWithQuery)

  if (path === '/') {
    response.statusCode = 200
    response.setHeader('Content-Type', 'text/html;charset=utf-8')
    response.write(`二哈`)
    response.end()
  } else if (path === '/x') {
    response.statusCode = 200
    response.setHeader('Content-Type', 'text/css;charset=utf-8')
    response.write(`body{color: red;}\n`)
    response.end()
  } else {
    response.statusCode = 404
    response.setHeader('Content-Type', 'text/html;charset=utf-8')
    response.write(`你输入的路径不存在对应的内容`)
    response.end()
  }

  /******** 代码结束,下面不要看 ************/
})
server.listen(port)
console.log('监听 ' + port + ' 成功\n请用在空中转体720度然后用电饭煲打开 http://localhost:' + port)

Logic:

  • According to the path and return back a string
  • Every time when you receive a request, the middle part code will be executed
  • Use if else to choose the path, and return the response
  • Return status code 200 if it is a known path
  • Return status code 404 if the is an unknown path
  • Content-Type represents Type/Grammar of the content
  • response.write() represents the return content
  • response.end() represents when execute this code, the response will send to the user immediately

IMPORTANT!!!

The function of file suffix is to facilitate the identification of applications on the operating system, front-end request or response code in the identification of files don’t rely on suffixes

For example, as long as the content of the file is CSS code, the file suffix can be un.css

Attention

Checking request details in Network Attention When Request When Response