Q: Where is the hosts file? A:

  • On Windows, hosts is located at C:\Windows\System32\drivers\etc\hosts.
  • On macOS/Linux, hosts is located in /etc/hosts

Url-uniform Resource Locator Indicates the Uniform Resource Locator

The standard format of a Uniform resource locator is as follows:

URI = scheme: / / authority path? [query] [# fragments] [protocol type] : / / [server address] : [port] / [resource hierarchy UNIX file path] [name]? [query]#[fragment ID]Copy the code

Protocol type: HTTP, HTTPS, FTP… Server ADDRESS: Domain name or IP Address Port: port number that provides services Path: UNIX file path Query: Query String, also known as query parameters, can be received and recognized by the server Fragment/anchor: fragment Can be used to locate a specified location on the page

View local IP Settings

windows: ipconfig

mac: ifconfig

The loopback: 127.0.0.1 localhost

Port to Port

Computers communicate according to the TCP/IP protocol of the Internet transport layer, and different protocols correspond to different ports.

  • 0-1023: indicates the official port. The application and port combination are recorded in the PORT allocation list of the IANA
  • 1024-49151: Unofficial application and port combinations are not in IANA’s port assignment list
  • 49152-65535: By definition, this port is in the dynamic port range. No port can be registered and occupied.

Common port: HTTP: 80, alternate port: 8080 HTTPS: 443 FTP: 21 (control port, common port), 20 (default data port) SSH: 22 DNS: 53

Port is occupied is a very common situation, if found to be occupied on the line.

The ping tool

Ping uses ICMP returned packets to calculate the packet loss rate and RTT usage of the network

  - Ping the specified host:
    ping host

  - Ping a host a specific number of times:
    ping -c count host
Copy the code

DNS – Domain Name System

Domain names correspond to IP addresses for easy memorization. Load balancing: A domain name corresponds to multiple IP addresses to prevent excessive pressure. Shared host: An IP address corresponds to multiple domain names.

Nslookup queries domain names

Sends a request to the DNS to obtain the domain name and corresponding IP address.

The theoretical order of web page requests: HTML -> CSS -> JS web page content requests in order, so the web page content should be arranged reasonably.

Domain level:

A domain name is made up of several parts, usually joined together and separated by dots.

  • Top-level domain name:

Top-level domains (TLD) are the highest level of domain names, and each domain name ends in a top-level domain. .com.cn org.gov are top-level domain names

  • Subdomain:

Secondary domain name:

Located to the left of the top-level domain name. For Wikipedia.org: Wikipedia is a ‘secondary domain name’. Wikipedia.org can be called a level 1 domain.

Level 3 Domain name:

Located to the left of the secondary domain name.

For zh.wikipedia.org: zh is a ‘level 3 domain name’. And zh.wikipedia.org can be called a ‘secondary domain’


All sites with WWW will be referred to their domain name by default.

Query String Query parameter

Form parameters in GET mode: start with? Each parameter is separated by “&”, and then “=” to separate the parameter name and data. Usually, the URL encoding is UTF8 to avoid character conflicts

Fragment anchor/Fragment

Fragment. Use the “#” character as the starting point to quickly navigate to the specified location on the page (anchor point). Anchors are received by the browser, but not sent to the server.

Chinese in URL

In fact, the URL itself does not support Chinese characters. The Chinese characters of the URL are transcoded according to UTF-8, and each byte is preceded by % to form the URL encoding. In the browser address bar, the browser thinks that % is an escape character, and the browser takes out the code between % and %, decodes it, and passes it to the back end, which decodes it again. The encodeURI() method escapes the string to form the URL encoding. The decodeURI() method restores the escaped URL fragment as the reverse of encodeURI.

HTTP

HTTP specification document: RFC2612

HTTP request and response

The content of the reference: www.liaoxuefeng.com/wiki/101695…

User-Agent -> Server

The difference between a GET request and a POST request is that a GET request sends only header data to the server. In addition to header data, a POST request comes with a body that contains user data. In addition, GET is a plaintext request, while POST requests are not displayed externally.

HTTP Request (RFC 2612 Chapter5)

The request format:

  • Request header
Request verb path and query parameters protocol name/version Host: domain name or IP address Accept: acceptable Content, e.g. text/ HTML <br> content-type: format of Content contained in request bodyCopy the code

Request verb: GET/POST/PUT/PATCH/DELETE

  • Request body

GET requests generally do not contain a body, and posts contain the uploaded content.

HTTP Response (RFC 2612 Chapter6)

The Response format:

  • Response header
Protocol name/Version Status code Status String Content-Type: Response Body Indicates the format of the ContentCopy the code
  • Response body

Response content, that is, downloaded content

Status Code Indicates the Status Code

cURL

The curl command is a file transfer tool that works on the command line using URL rules. Curl supports uploading and downloading files, so it is a comprehensive transfer tool. Curl is a powerful tool that supports HTTP, HTTPS, FTP and many other protocols. It also supports POST, cookies, authentication, downloading partial files from specified offsets, user agent strings, speed limits, file sizes, and progress bars. Curl can help automate web processing and data retrieval.

Curl Sends an HTTP request

You can learn curl from the following documentation: man.linuxde.net/curl

Curl -v: indicates the verbose mode

 -v, --verbose
              Makes curl verbose during the operation.  Useful  for  debugging
              and  seeing  what's  going  on "under the hood". A line starting
              with '>' means "header data" sent by  curl,  '<'  means  "header
              data"  received  by  curl  that is hidden in normal cases, and a
              line starting with '*' means additional info provided by curl.

              If you only want HTTP headers in the output, -i, --include might
              be the option you're looking for.

              If  you think this option still doesn't give you enough details,
              consider using --trace or --trace-ascii instead.

              Use -s, --silent to make curl really quiet.
Copy the code

In short, verbose mode provides detailed request content. > indicates that header data is sent. < Indicates that header data is received from the server

Curl’s results show that:

  1. DNS requests the IP address corresponding to the domain name, which is accessed through port 80.
  2. The TCP connection is established
  3. Send a request containing the access method (GET), host, user-agent (curl), and what to expect back.* / *Represents that all content is accepted)
  4. Get feedback, status code 200 OK
  5. The feedback includes header data and the actual content of the web page
  6. Disabling a TCP Connection
  7. End of the program

Since this visit does not include WWW, Baidu only returns meta. In the same way, go to bing.com and get 302 Found. Go to www.bing.com and get 301 Moved Permanently.

E.g. – X requests verb curl -x POST at http://localhost:8888/xxx? test=hi

-h ‘Name: Value or – headers’ Name: Value: set the request header e.g. Curl -h’ Accept: text/HTML ‘http://localhost:8888/xxx? test=hi

Node.js implements a simple request and response

Reference: nodejs.org/zh-cn/docs/…

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

if(! port) {console.log('Please set a Port number: ');
    process.exit(1);
}

var server = http.createServer(
    function (request, response) {
        /* Basic parameters */
        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;
        /* Receive the request and print its path and parameters */
        console.log('New request - Path and Query: ' + pathWithQuery);
        /* Return different response headers */ based on the request path
        if (path === '/') {
            response.statusCode = 200;
            response.setHeader('Content-Type'.'text/html; charset=utf-8');
            response.write(`root`);
            response.end();
        }
        else if (path === '/x') {
            response.statusCode = 200;
            response.setHeader('Content-Type'.'text/css; charset=utf-8');
            response.write(`body{color: red; } `);
            response.end();
        }
        else {
            response.statusCode = 404;
            response.setHeader('Content-Type'.'text/html; charset=utf-8');
            response.write(`The path does not have any content`);
            response.end();
        }
    }
)

server.listen(port);
console.log('Listen ' + port + ' successfully. You can open the page through http://localhost:' + port);
Copy the code

Results:

A request was sent through Chrome, Safari, and Curl

Node.js read request

  • Read request verbs:request.method
  • Read the URL (with query parameters) :request.url
  • Read the pure path path: ‘request.path
  • Read query parameters:request.query
  • Read the request header:request.headers[]

Node.js sets the response

  • Setting status code:response.statusCode = <codeNumber>;
  • Set the response header:response.setHeader('Name', 'Value');
  • Set the response body:response.write('Content');

Resolve the port occupation problem on the Mac

If the service fails to be started, the port may be occupied.

  1. You can uselsof -i tcp:<port>Query the services enabled on a specified port.

  2. usekill <PID>Disable the process to remove the port occupation.