This is the 20th day of my participation in the Genwen Challenge

The server

Introduction to the

Before we wrote the page, if you want to show yourself, directly open the browser. If you want someone else to see it, send it to him. But if there are too many people to view, it is not possible to send them one by one. So set up a server. This server is on the public network. As long as you put your website on this server, others can access it through the public network. (You need a domain name: a domain name is the ID of your server on the public network.) In short, a server is a machine that provides services. We are divided into different servers according to their different uses. We are looking at the simplest Web server.

NodeJS server

Previously, there was a server.js file, which was a simple NodeJS server.

The HTTP protocol

Introduction to the

Browsers: Chrome, Internet Explorer, Firefox, Edge, etc. These are all applications. Their level is the same as QQ.

Server: NodeJS server.

The browser makes a request for the content on the server. The specification used between them is called the HTTP protocol.

The HTTP protocol specifies how browsers make requests.

The HTTP protocol specifies how the server responds.

The HTTP request

If HTTP protocol is a class. The HTTP request is an instantiated object.

An HTTP request consists of four parts: the first line, the header, the blank line, and the body of the request

First line of request:

Request header:

Request blank line: a blank line that does nothing to separate the request header from the request body.

Request body: None

URL

Introduction to the

URL refers to uniform resource locator.

When we want to visit a website. The first step is to enter the url. The URL is a URL.

Demo:

https://login.taobao.com/member/login.jhtml?from=taobaoindex&f=top&style=&sub=true&redirect_url=https%3A%2F%2Fi.taobao.com%2Fm Y_taobao. HTM % 3 FSPM % 3 da21bo. 2017.1997525045.1.5 af911d9IjloSn # aaa
Copy the code

As above, it is a URL string.

A complete URL contains: protocol, domain name, port, pathName, search(? + query), hash

When the browser enters the URL, press Enter

When a URL string is entered in the browser’s address bar and press Enter, the browser parses the URL. Determine the domain name to which the packet is sent according to the RULES of the HTTP protocol.

1Parse to URL objects2Obtain the domain name and resolve it into an IP address1Obtain the corresponding IP address from the browser cache2Obtain the corresponding IP address from the system cache3Obtain the corresponding IP address from the route cache4Obtain the IP address from DNS3Make an HTTP request based on the obtained IP address4The request arrives at the server5Server reply link established6The server process returns data based on the link7Disconnect the link8The browser gets the data and starts rendering9In the rendering process, external chain tags such as link tag, script tag, IMG tag, video tag, audio tag will send HTTP request again.Copy the code

The cache

When the browser sends a request that matches the data URL of a previous request, it uses the data of the previous request rather than resending the request.

The characteristics of NodeJS

Introduction to the

NodeJS is not a language, it is a development platform. The language used is JS.

NodeJS is a lightweight, efficient, non-blocking I/O development environment based on ChromeV8 engine.

NPM is the largest open source library ecosystem in the world.

hello world

There is a JS file:

console.log("hello world");
Copy the code

Single thread

When a task is executed, there is only one thread to execute it. This is called “single threading”.

Non-blocking I/O

I: input O:output Output

Memory is fast and disk speed is relatively slow. If data is fetched from memory to disk, it is called “output” from memory. The reverse is called “input”. The speed is determined by the disk speed. Is the thread waiting at this point? If you wait, it is called blocking I/O. Not waiting is called non-blocking I/O.

event-driven

Because of non-blocking I/O. As a result, the thread leaves, and subsequent tasks cannot be executed after I/O completion. When I/O completes, an event is emitted. The event driver continues execution. This is called “event-driven.”

A small case

There is a clinic (NodeJS server) there is a doctor in the clinic (single thread) there are N nurses in the clinic (memory) there is a warehouse in the clinic (disk) one day comes10Patients (HTTP requests sent by the front end). The doctor dealt with the symptoms of the first patient. (Processing process includes: diagnosis, prescription, prescription, doctor's advice) diagnosis, doctors can be done immediately, prescribing doctors can also be done immediately. The medicine is filled by a nurse who goes to the warehouse to get the medicine. Is an I/O operation. The doctor does not wait for the nurse to return from filling the medicine and cannot order it immediately. Instead, treat the next patient's symptoms immediately. Continue with the above process. At that moment, one of the nurses came back. Nurses don't pull doctors over for orders. It was the nurses who stood in line. The doctor continues processing, processing to the nurse to carry out the nurse's corresponding patient's orders.Copy the code

Modularity in NodeJS

There are two types of modules in NodeJS, one is the core module. One is third-party modules.

The core module

Core modules are the ones that come with NodeJS.

List on the left of official website

Third-party modules

Third party modules are non-core modules.

Definition module

In NodeJS, a JS file is a module.

Exposure to function

module.exports = sum; // The sum function is exposed
module.exports.sum = sum; The sum attribute of the object is the sum function
exports.sum = sum; / / same as above
Copy the code

The introduction of the module

Importing third-party modules: In NodeJS, the require function is also used to import content

Demo:

// Introduce the exposure function in B.js
var b = require("./b.js"); 
Copy the code

You can import it without the.js suffix because NodeJS is sensitive to JS files.

Introducing core modules

The core module has only one name and is in memory, so the path cannot be written. Just write your name.

require("http");
Copy the code

Node_modules folders

This folder is used to store node’s third-party modules.

Its features:

1Simply drop a third-party module into this folder and you can import it as a core module2It can exist in a multi-tiered directory and when you're looking for a file you look for node_modules in its parent and if there's one, you go there and if there's none, you go up one level and see if there's any node_modules up thereCopy the code

NodeJS server

The HTTP module

This module is used to create HTTP servers

Introduction:

var http = require("http");
Copy the code

Call API:

var server = http.createServer(handler);
Copy the code

Handler: The response function that is called when a request is sent to the server.

Listening port number:

server.listen(3000);
Copy the code

request

The handler in the createServer method is a function that takes two parameters.

The req and res.

Req represents the front-end request object on which all front-end data resides.

Important attributes:

Req. url URL string Req. method Method Method string (uppercase)Copy the code

response

Res represents the back-end response object, and the methods on that object perform how to respond to the data.

Important properties and methods:

Res.setheader is used to set the response header res.end is used to end the request and return data only the accepted string and buffer res.statuscode is used to set the response statusCodeCopy the code