Node server with AJAX



We wrote the page before, if you want to show it to yourself, you can open it directly with the browser. If you want to show it to someone else, send it to him. But if there are too many people watching, it is impossible to send one by one at this time. 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. (Have a domain name: a domain name refers to an id of your server on the public network)
Simply put, a server is a machine used to provide services.
We divide the server into different servers according to the purpose of the server. We are learning about the simplest web server.

NodeJS server

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

HTTP protocol


Browser: chrome browser, Internet Explorer, Firefox, edge browser, etc. These are some apps. Their level is the same as qq.

Server: NodeJS server.

The browser makes a request for 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 dictates how the server should respond.

HTTP request

If the HTTP protocol is a class. Then the HTTP request is the instantiated object.

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

Request first line:

Request header:

Request blank line: just a blank line with no content. The function is to separate the request header from the request body.

Request body: none



URL refers to Uniform Resource Locator.

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


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 and presses enter

When you enter the URL string in the browser's address bar and press Enter, the browser parses the URL. According to the rules of the HTTP protocol, it decides which domain name to send to.

1 parsed into URL object
2 Get the domain name and resolve it into IP address
    1 Get the corresponding from the browser cache ip address
    2 Get the corresponding from the system cache ip address
    3 Get the corresponding from the route cache ip address
    4 from DNS get the corresponding ip address 
3 According to the obtained ip address issued HTTP ask 
4 the request arrives at the server
5 Server Reply Link Established
6 Server processing returns data according to the link 
7 disconnect
8 The browser gets the data and starts rendering
9 encountered during rendering link Label, script Label, img Label, video Label, audio Tags and other external chain tags will be re-issued HTTP ask.


When the request sent by the browser is consistent with the previously requested data URL, the previously requested data is used instead of resending the request.

Features of NodeJS


Official website:
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 the Chrome V8 engine.

NPM is the world's largest ecosystem of open source libraries.

hello world

There is a JS file:

console.log("hello world");

single thread

When executing a task, there is only one thread to execute. This is called "single thread".

Non-blocking I/O

I: input input O:output output

Memory runs fast, and disk spins relatively slowly. If data is fetched from memory to disk, this is called "exporting" from memory. It's called "input" in turn. The speed is based on the rotational speed of the disk. Is the thread waiting at this point? If you wait, it is called blocking I/O. Do not wait, called non-blocking I/O.

event driven

Because of non-blocking I/O. Causes the thread to leave, and subsequent tasks after the I/O is completed cannot continue to execute. When the I/O completes, an event is fired. The event driver continues execution. This is called "event driven".

Small case

There is a clinic ( NodeJS server)
There is a doctor in the clinic(single thread)
in the clinic N nurses (memory)
There are warehouses (disks) in the clinic
 10 patients came one day (sent from the front end HTTP ask).
The doctor deals with the symptoms of the first patient. (The processing flow includes: diagnosis, prescription, medication, doctor's order)
Diagnosis, the doctor can do it immediately, and the prescribing doctor can do it immediately. Prescribing medicine is by the nurse going to the warehouse to get the medicine. Is an I/O operate.
The doctor will not wait for the nurse to come back with the medicine, and cannot give the doctor's order immediately. Instead, deal with the next patient's symptoms immediately. Continue the above process.
At this time, a certain nurse came back. Nurses don't pull doctors over to prescribe. Instead, nurses line up. The doctor continues to process, and the nurse's doctor's order for the corresponding patient is executed only after the nurse is processed.

Modularity in NodeJS

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

core module

The so-called core module refers to the module that comes with NodeJS.

List on the left side of the official website

third-party modules

The so-called third-party modules refer to non-core modules.

define module

In NodeJS, a JS file is a module.

Expose function

module.exports = sum; // Exposed is the sum function
module.exports.sum = sum; // What is exposed is an object, the sum property of the object is the sum function
exports.sum = sum; // Ditto

import module

Import third-party modules:
In NodeJS, also use the require function to import content


// Introduce exposed functions in b.js
var b = require("./b.js"); 

It is not necessary to write the .js suffix when importing, because NodeJS is sensitive to js files.

import core module

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


node_modules folder

This folder is dedicated to storing third-party modules of node.

Its features:

1 As long as a third-party module is placed in this folder, it can be imported like a core module
2 It can exist in a multi-level directory. When looking for a file, it will first look for the file in the same level. node_modules If there is, go in and look for it; if not, go to the previous level to find out if there is node_modules up 

NodeJS server

HTTP module

This module is used to create HTTP server


var http = require("http");

Call API:

var server = http.createServer(handler);

handler: The response function called when a request is sent to this server.

Listening port number:



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

req and res.

Req represents the front-end request object, and all front-end data is on this object.

Important properties:

req.url  URL string
req.method Method method string (uppercase)


Res represents the back-end response object, and how to respond to the data is performed by the method of the object.

Important properties and methods:

res.setHeader  Used to set response headers
res.end     used to end the request and return data can only receive strings and buffer
res.statusCode  Used to set the response status code

at last

The author publishes good articles every week for everyone to learn, and everyone is welcome to pay attention.

WeChat search [Front-end daily skills] Follow the official account, writing is not easy, I hope to like 👍️ add favorites ❤️ and forward.

Tags: Javascript node.js Front-end

Posted by dannon on Wed, 01 Jun 2022 23:55:09 +0530