This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

TIP 👉 A hero is a man who has great ambition, good policy in his belly, the opportunity to contain the universe and swallow the ambition of heaven and earth. — Romance of The Three Kingdoms

preface

How to create a TCP service using Nodejs?

Before we do that, we need to understand the concept of Socket.

Socket is an abstract layer between the application layer and the transport layer. It abstracts the complex operation of TCP/IP layer into several simple interfaces for the application layer to call the implemented process to communicate in the network, such as CREATE, listen, accept, connect, Read and write and so on.

Node has various network related modules. HTTP is the application layer module, which mainly encodes and decodes data according to specific protocols. Net is the transmission layer module, mainly responsible for the transmission of encoded application layer data; HTTPS is a comprehensive module (covering HTTP, TLS, crypto, etc.), mainly used to ensure data security

HTTP

HyperText Transfer Protocol (HTTP) is used to Transfer HyperText from WWW server to local browser. It can make browsers more efficient and reduce network traffic. It not only ensures that the computer transfers the hypertext document correctly and quickly, but also determines which parts of the document to transfer and which parts of the content to display first (e.g. text before graphics).

HTTP is an application-layer communication protocol between a client browser or other program and a Web server. Hypertext information is stored in Web servers on the Internet. Clients need to transfer the hypertext information to be accessed through HTTP protocol. HTTP contains commands and transmission information. It can be used not only for Web access, but also for communication between other Internet/Intranet applications. In this way, hypermedia access of various application resources can be integrated.

The web address we type into the address bar of our browser is called a Uniform Resource Locator (URL). Just as every home has a house address, every web page has an Internet address. When you type a URL into the browser’s address box or click on a hyperlink, the URL determines the address to visit. Browsers use hypertext Transfer Protocol (HTTP) to extract the code from a Web server site and translate it into a beautiful Web page.

What does a complete HTTP communication look like?

  1. Establishing a TCP Connection

Before HTTP work can begin, the client first establishes a connection with the server over the network, which is done through TCP. HTTP is an application layer protocol with a higher level than TCP. According to the rules, a connection with a higher layer protocol can be established only after a lower layer protocol is established. Therefore, a TCP connection must be established first.

  1. The client sends a request command to the server

Once a TCP connection is established, the client sends a request command to the server.

For example, GET/info HTTP/1.1

  1. The client sends a request header

After sending its request command, the client sends some additional information to the server in the form of a header. The client then sends a blank line to inform the server that it has finished sending the header.

  1. Server reply

After the client sends a request to the server, the server returns a response.

For example, HTTP/1.1 200 OK

The first part of the response is the protocol version number and response status code

  1. The server returns a response header

Just as the client sends information about itself along with the request, the server sends data about itself and the requested document to the user along with the response;

  1. The server sends data to the client

After the server sends the header to the client, it ends by sending a blank line to indicate that the header was sent there, and then it sends the actual data requested by the user in the format described in the Content-Type response header.

  1. The server closes the TCP connection

Normally, once the server returns the request data to the client, it closes the TCP Connection. Then, if the client or server adds the line Connection:keep-alive to its header, the TCP Connection will remain open after being sent. The client can continue to send requests over the same connection. Keeping the connection saves the time required to establish a new connection for each request and saves network bandwidth.

What are the features of HTTP?

  1. Communication is achieved through the exchange of requests and responses

According to the protocol, the request is made from the client and the server responds to the request and returns.

  1. stateless

HTTP is a stateless protocol. At the HTTP level, the protocol does not persist requests or responses that have been sent

  1. Use cookies for state management

The header returned by the server may carry set-cookie, and when the client receives the response, the Cookie will be planted locally. The next time a request is sent to the server, it carries these cookies.

  1. Locate resources by URL

Here’s a distinction between urIs and urls.

URI: A Uniform resource Identifier (URI). For example, if your id number is XXXXXXX and unique among all people, this id number identifies you, then it is a URI

URL: Unified resource locator, such as Beijing/Chaoyang district/XXXX/XXXX/XXXXX, through this string of information can locate you, then this is the URL

A URL is somewhat similar to a URI implemented through location.

There is a parent class called URI that uniquely identifies an ID. Some people prefer to inherit urIs through location; Some people prefer to inherit urIs by name.

  1. Identify your intentions in a variety of ways

This refers to various HTTP methods, such as GET, POST, PUT, and DELETE.

  1. A persistent connection

In the original version of the HTTP protocol, TCP connections were disconnected for every HTTP communication, which added a lot of unnecessary connection establishment overhead.

To address the above TCP connection problem, HTTP/1.1 supports persistent connections. The TCP connection is maintained as long as neither end explicitly disconnects. To establish a TCP connection for multiple requests and responses. In HTTP/1.1, all connections are persistent by default.

This means that by default, establishing a TCP Connection does not break. Only by declaring Connection: close in the request header will the Connection be closed upon completion of the request.

  1. Pipeline mechanism

Pipelining was introduced in version 1.1, which allows clients to send multiple requests simultaneously over the same TCP connection.

For example, the client needs to request two resources. In the past, in the same TCP connection, first send A request, and then wait for the server to respond, and then send A request B. The pipeline mechanism allows the browser to make both A and B requests at the same time, but the server responds to A and B requests in the same order.

However, modern browsers generally do not enable this configuration, and this mechanism can cause queue headers to block. Because responses are sequential, if the first HTTP request in a TCP connection responds very slowly, it blocks the response to subsequent HTTP requests.

So by default, a TCP connection sends only one HTTP request at a time.

Some students will ask, how did I hear that Chrome supports up to 6 requests for the same domain name?

That’s because Chrome supports up to six SIMULTANEOUS TCP connections.

So what are the main differences between HTTP 1.0/1.1/2.0 and concurrent requests?

  1. HTTP / 1.0

Each TCP connection can send only one request. When the server responds, the TCP connection is closed and the TCP connection needs to be established again for the next request.

  1. HTTP / 1.1

Continuous connections are used by default (TCP connections are not closed by default and can be reused by multiple requests without declaring Connection: keep-alive).

Added the pipeline mechanism, in the same TCP connection, allows multiple requests to be sent at the same time, increases the concurrency, and further improves the efficiency of the HTTP protocol.

But in the same TCP connection, all data communication is in order. The response is slow and many requests queue up, causing “queue congestion”.

  1. HTTP / 2.0

Add duplex mode, that is, not only the client can send multiple requests at the same time, the server can also process multiple requests at the same time, to solve the problem of queue head congestion.

Multiplexing techniques are used to concurrently process multiple requests for the same connection, and the number of concurrent requests is orders of magnitude larger than HTTP1.1.

Add server push function, without request server to actively send data to the client.

All kinds of Headers

Cache-Control

You can manipulate how the Cache works by specifying an instruction for the header field cache-control.

  1. Cache-Control: public

When you specify the use of a public directive, it makes it clear that other users can take advantage of the cache.

  1. Cache-Control: private

When a private directive is specified, the response only takes a specific user as an object, as opposed to the behavior of a public directive. The cache server provides resource caching for this particular user, and the proxy server does not return requests from other users to the cache.

  1. Cache-Control: no-cache

Resources can be stored on the client, and each time you have to go to the server to do an expiration check to decide whether to fetch new resources from the server (200) or use the client cache (304). This is known as negotiated caching.

  1. Cache-Control: no-store

Never store resources on the client side, always go to the original server for resources.

  1. Cache-control: max-age=604800 (unit: seconds)

When a client sends a request that contains a max-age directive, the client receives the cached resource if the cache time value is determined to be smaller than the specified time. In addition, when you specify a value of 0 for max-age, the cache server usually needs to forward requests to the source server.

HTTP/1.1 caching servers will preferentially process max-age directives when both Expires header fields exist and ignore the Expires header field

  1. Cache-control: s-maxage=604800 (unit: seconds)

The s-maxage directive performs the same functions as the Max-age directive, except that the S-maxage directive only works with a common cache server (typically a proxy) that can be used by multiple users.

When the S-maxage directive is used, processing of the Expires header field and the Max-age directive is ignored

Connection

  1. Connection: close

The default connection for HTTP/1.1 is persistent. When the server wants to explicitly disconnect, specify the value of the Connection header field as close.

  1. Connection: Keep-Alive

The default connection for HTTP versions prior to HTTP/1.1 was non-persistent. To do this, if you want to maintain a persistent Connection over older versions of HTTP, you need to specify the value of the Connection header field as keep-alive.

Date

Indicates the date and time when the HTTP packet is created.

Date: Mon, 10 Jul 2021 15:50:06 GMT

The HTTP/1.1 protocol uses the date-time format specified in RFC1123.

Pragma

Pragma header fields are historical legacy fields prior to HTTP/1.1 and are defined only as backward compatibility with HTTP/1.0.

  1. Pragma: no-cache

This header field is a generic header field, but is used only in requests sent by clients, requiring all intermediate servers not to return cached resources.

If all intermediate servers can use HTTP/1.1 as a benchmark, cache-control: no-cache is the best way to specify the Cache. However, it is not practical to know the HTTP protocol version used by all intermediate servers, so the request will contain both the following header fields:


Cache-Control: no-cache

Pragma: no-cache

Copy the code

Accept

  1. Accept: text/html, application/xhtml+xml, application/xml;

The Accept header field informs the server of the media types that the user agent can handle and the relative priority of the media types. You can specify multiple media types at once using the type/subtype form.

  1. Accept-Encoding: gzip, deflate

The accept-Encoding header field is used to tell the server the content Encoding supported by the user agent and the priority of the content Encoding, and multiple content Encoding can be specified at once

You can also use the asterisk (*) as a wildcard to specify any encoding format.

Gzip indicates that the entity is encoded in GNU ZIP

Compress indicates that the entity uses a Unix file compression program

Deflate indicates that the entity is compressed in zlib format

Identity indicates that the entity is not encoded, which is used by default when there is no content-Encoding header field

Host

  1. Host: www.baidu.com
  • Tells the server the Internet host and port number of the requested resource.

  • The Host header field is the only HTTP/1.1 header field that must be included in the request.

  • If the server does not have a Host name, send a null Host:.

If-Modified-Since

A request header field in the form of if-xxx is called a conditional request. When the server receives a conditional request, it will execute the request only if it determines that the specified condition is true.

  1. If-Modified-Since: Mon, 10 Jul 2021 15:50:06 GMT

Used to verify the validity of local resources owned by the agent or client.

If none of the requested resources has been updated after the date or time specified for the if-Modified-since field value, a response with status code 304 Not Modified is returned

ETag

  1. ETag: “aaaa-1234”

The header field ETag tells the client the entity identity. It is a way to uniquely identify a resource as a string. The server assigns an ETag value to each resource.

In addition, the ETag value needs to be updated when the resource is updated. When ETag values are generated, there is no uniform algorithmic rule, but only allocation by the server.

If-None-Match

  1. If-None-Match: “lubai”

When the entity tag (ETag) value used to specify the if-none-match field value is inconsistent with the ETag of the requested resource, it tells the server to process the request.

User-Agent

  1. The user-agent: Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

The header field user-Agent communicates information to the server such as the browser that created the request and the name of the User Agent.

When a web crawler initiates a request, it is possible to add the email address of the crawler author to the field. Also, if the request goes through a proxy, the proxy server name is likely to be added in the middle.

Allow

  1. Allow: GET, HEAD

The header field Allow is used to inform the client that all HTTP methods of the resource specified by request-URI can be supported.

When receiving an unsupported HTTP Method, the server returns a response with the status code 405 Method Not Allowed. At the same time, all supported HTTP methods are written to the header field Allow and returned.

Content-Encoding

  1. Content-Encoding: gzip

The header field content-Encoding tells the client which Content Encoding method the server chooses for the body of the entity. Content encoding refers to compression without loss of entity information.

Content-Type

  1. Content-Type: text/html; charset=UTF-8

The header field content-Type specifies the media Type of the object in the entity body. As with the header field Accept, the field value is assigned as type/subtype.

Expires

  1. Expires: Mon, 10 Jul 2021 15:50:06 GMT

The header field Expires tells the client when the resource Expires.

The cache server responds to a request with a cache after receiving a response containing the header field Expires. A copy of the response is stored until the Expires field value is specified. When the specified time passes, the cache server turns to the source server to request the resource when the request is sent.

Set-Cookie

  1. Set-Cookie: userId=11111; expires=Mon, 10 Jul 20121 15:50:06 GMT; path=/;
  • NAME=VALUE: Indicates the cookie NAME and VALUE

  • Expires =DATE: Indicates the expiration DATE of the Cookie (defaults to before the browser closes if it is not specified explicitly)

  • Path = path: The file directory used to limit the range of cookies to be sent.

  • Domain = domain name: domain name for which the cookie is valid (if not specified, the default is the domain name of the server that creates the cookie)

  • Secure: Cookies are sent only for Secure HTTPS communication

  • HttpOnly: Disables cookies from being accessed by JavaScript scripts

How do I create HTTP services using NodeJs?

  1. http-server.js
const http = require('http')

http.createServer(function (req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    })
    res.end('Hello World')
}).listen(80.'127.0.0.1')
console.log('Server running at http://127.0.0.1:80/)
Copy the code
  1. Browser access

http://127.0.0.1:80/

  1. Use the curl access

The curl -v http://127.0.0.1:80

Take a look at the request message

// Triple handshake * underway URL to: http://127.0.0.1:80/ * Trying 127.0.0.1... * TCP_NODELAY set * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0) 127.0.0.1:80 > curl/7.54.0 > Accept: */* > text/plain < Date: Wed, 04 Aug 2021 15:55:55 GMT < Connection: keep-alive < Keep-Alive: timeout=5 < Transfer-Encoding: Chunked < * Connection #0 to host 127.0.0.1 left intact Hello World%Copy the code
  1. htttp-client.js
const http = require('http')

const options = {
    hostname: '127.0.0.1'.port: 80.path: '/'.method: 'GET'
}

const req = http.request(options, (res) = > {
    console.log(`Status=${res.statusCode}, Headers=The ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8')
    res.on('data'.(data) = > {
        console.log(data)
    })
})

req.end()
Copy the code

“Feel free to discuss in the comments section.”

Hope to finish watching friends can give a thumbs-up, encourage once