Introduction to Node.js [Note 1]

Introduction to Node.js

Node.js was released in May 2009 and developed by Ryan Dahl. It is a JavaScript running environment based on the Chrome V8 engine. It uses an event-driven, non-blocking I/O model to allow JavaScript to run on the server-side development platform. It Let JavaScript become a scripting language on par with server-side languages ​​such as PHP, Python, Perl, and Ruby.
Node.js optimizes some special use cases and provides alternative APIs to make V8 run better in non-browser environments. The V8 engine executes Javascript very quickly and has very good performance. It is a platform built on the Chrome JavaScript runtime. Used to easily build network applications with fast response and easy expansion.

  • Front-end core technology system

Insert image description here

  • Node.js can be used as a server [small program, app, website, game]
  • The learning content is as follows: [More knowledge points, more difficult]

Insert image description here

  1. computer structure
  2. Program running process
  3. process and thread
  4. Synchronous and asynchronous
  5. IP and port

Why learn Node.js?

  1. The three major frameworks of front-end development are closely related to it: Vue, React, and Angular
    Insert image description here
  2. Allow others to access web pages we write
  3. Lay the foundation for subsequent framework learning

What is Node.js?

Node.js is an open-source, cross-playform JavaScript runtime environment. [Taken from https://nodejs.org/en/ official website]
Node.js is an open-source, cross-playform JavaScript runtime environment.
Popular explanation: Node.js is an application , a piece of software that can run JavaScript.
Myth: Node.js is a programming language. Node.js is the new version of JavaScript.

The role of Node.js

Insert image description here

1. Develop server applications

Insert image description hereWeb page file composition: HTML control structure, CSS control style, JavaScript control interaction and effects
nodejs runs on the server

The server saves the written HTML, CSS, and JavaScript files;
the user sends a request to the server through the URL;
the server returns the resources to the browser, and the browser parses the resources to display the website pages.

2. Development tool applications

Insert image description here

3. Develop desktop applications

Insert image description here

  • VSCode: code editor
  • Figma: design tool
  • Postman: interface testing tool
  • electron: The above three software all rely on the electron framework, which is developed with the help of node.js.
    Insert image description here

Node.js installation

Link: Node.js official website [https://nodejs.org/en]
Insert image description here

  • LTS: long-term maintenance version

Win+R→Input cmd→enter: Open the command prompt tool [Command Line Tool]
Enter node -v to monitor whether node.js is successfully installed and view the version
Insert image description here
diagram: [C:\Users\Zyf] is the work of the command line Directory; node is the command name, -v is the command parameter.

Get to know command line tools

The program can be run in the command line tool.
The structure of the command consists of the command name and the command parameters [the command parameters can be 0 or more]

How to view the contents of D:/Program Files using the command line?

#切换工作目录到D盘命令 d:
#查看D盘内容命令 dir
#改变文件夹命令 cd 【切换工作目录】
#将工作目录切换到上一级命令 cd ..

Insert image description here

illustrate operate
Switch drive letter C: D:
Switch working directory cd
View catalog files dir
  • dir \s to view the contents of the current working directory, including the contents of subfolders, you can terminate the command with Ctrl+C
  • Run the js file in the command line tool: Run the node filename.js command in the current file directory to run the js file.

Node.js considerations

  1. BOM and DOM APIs cannot be used in Node.js, but console and timer APIs can be used.
  2. The top-level object in Node.js is global, and you can also use globalThis to access the top-level object.

Insert image description hereJS core syntax: variable declaration, loop control, object function declaration, etc.

//BOM 
console.log(window);     //ReferenceError: window is not defined
console.log(history);    //ReferenceError: history is not defined
console.log(navigator);  //ReferenceError: navigator is not defined
console.log(location);   //ReferenceError: location is not defined
//DOM
console.log(document);   //ReferenceError: document is not defined
//AJAX
let xhr = new XMLHttpRequest();  //ReferenceError: XMLHttpRequest is not defined

console.log('one'); //one
setTimeout(() => {
    
    
     console.log('two');
 }, 1000); //two

//global 顶级对象
// console.log(global);
// console.log(globalThis);//ES2020
console.log(global === globalThis); //true

Introduction and creation of Buffer

1. Concept

Buffer is an object similar to an array, used to represent a fixed-length byte sequence;
Buffer is essentially a fixed-length memory space, specially used to process binary data.
Insert image description here

2.Features

  • Buffer size is fixed and cannot be adjusted
  • Buffer has good performance and can directly operate on computer memory.
  • The size of each element is 1 byte (byte) [1 byte= 8 bit]
    Insert image description here

3.Use

3.1 Create Buffer

How to create a Buffer in Node.js:

  1. Buffer.alloc
//创建一个长度为10字节的Buffer,相当于申请了10字节的内存空间,每个字节的值为0
let buf_1 = Buffer.alloc(10);	//Buffer是Nodejs内置模块,全局变量;alloc【分配】是方法
console.log(buf_1);				//结果为<Buffer 00 00 00 00 00 00 00 00 00 00>
  1. Buffer.allocUnsafe
//创建一个长度为10字节的Buffer,buffer中可能存在旧的数据,可能会影响执行结果,所以叫Unsafe
let buf_2 = Buffer.allocUnsafe(10);	
console.log(buf_2);	
  1. Buffer.from
//通过字符串创建buffer
let buf_3 = Buffer.from('hello');//字母转换成Unicode表的数字的16进制
console.log(buf_3);				//结果为<Buffer 68 65 6c 6c 6f>
//通过数组创建buffer
let buf_4 = Buffer.from([105,108,111,118,101,121,111,117]);
console.log(buf_4);				//结果为<Buffer 69 6c 6f 76 65 79 6f 75>

Unicode table compatible with ASCII table
Insert image description here

3.2 Conversion of Buffer and String

You can use the toString method to convert the Buffer into a string

let buf_4 = Buffer.from([105,108,111,118,101,121,111,117]);
console.log(buf_4.toString()); //结果为iloveyou

toString is converted according to utf-8 encoding by default.

3.2 Reading and writing Buffer

Buffer can process data directly through [ ]

//读取 []
// let buf_3 = Buffer.from('hello');
console.log(buf_3[0]);							//结果为104
console.log(buf_3[0].toString(2));				//结果为1101000,真实保存的值为01101000

//修改写入
buf_3[1] = 97;
console.log(buf_3.toString());					//结果为hallo

//溢出
buf_3[0] = 361; //255(10进制) => 1111 1111(2进制),大于255的舍弃高位的数字 0001 0110 1001(2进制) => 0110 1001(2进制) => 105(10进制) => 69(16进制)
console.log(buf_3);								//结果为<Buffer 69 61 6c 6c 6f>

//中文
let buf_5 = Buffer.from('你好');					//utf-8的一个中文字符占3个字节
console.log(buf_5);								//结果为<Buffer e4 bd a0 e5 a5 bd>

Note:
1. If the modified value exceeds 255, more than 8 bits of data will be discarded.
2. A UTF-8 character generally occupies 3 bytes.

Computer Basics

Basic components of a computer

Insert image description here

  • CPU: Central processing unit, the center of computer operation and control;
  • Memory: a medium for storing data, with fast reading and writing speeds, and data will be lost when power is turned off;
  • Hard disk: a medium for storing data. The reading and writing speed is slow and the data will not be lost when the power is turned off;
  • motherboard
  • Graphics card, sound card: processing signals
  • heat sink
  • Peripherals: mouse, keyboard, audio, etc.

Basic flow of program operation

Operating system: Windows, Linux, MacOS, is an application used to manage and schedule hardware resources. (The operating system needs to be installed on the computer’s hard disk)
Insert image description here
The basic process of computer startup:
Insert image description hereThe basic process of program startup:
Insert image description here

Programs are generally saved on the hard disk, and the software installation process is the process of writing the program to the hard disk.
When the program is running, it is loaded into the memory, and then the CPU reads and executes the program.

Processes and Threads

Process: [Program in progress], a process is an execution process of a program.
Windows systems can view processes through the task manager

Thread: A thread is an execution stream executed in a process. A thread belongs to a process.
A process can have one or more threads.
View all threads under a certain process: pslist -dmx pid number [pid number is the process id]
Note: Enter pslist. If the command is not recognized, it means that pslist is not installed on the current computer. Tools can be downloaded from the official Windows website: https://technet.microsoft.com/en-us/sysinternals/bb896682.aspx. After the download is complete, unzip it to the C:\Windows\System32 path for use, or It can also be configured into environment variables.

Insert image description hereInsert image description here

Guess you like

Origin blog.csdn.net/qq_41361442/article/details/130937120