Nodejs introduction and installation tutorial

1. Introduction to Nodejs

JavaScript was born in 1995, almost at the same time as the Internet; Node.js was born in 2009, about 15 years later than JavaScript.

Before Node.js, JavaScript could only run in the browser and be used as a web page script to add some special effects to the web page or communicate with the server. With Node.js, JavaScript can be separated from the browser and used directly on the computer like other programming languages. It can do whatever it wants without being restricted by the browser.

Node.js is not a new programming language, nor is it a JavaScript framework, it is a JavaScript runtime environment to support the execution of JavaScript code. In programming terms, Node.js is a JavaScript runtime (Runtime).
insert image description here
In addition to being used for web front-end programming (web programming), the current JavaScript can also do many things, such as:

  • Develop the background of the website, which is originally good at programming languages ​​​​such as PHP, Java, Python, and Ruby;
  • Develop GUI programs, which is what we often call computer software with interfaces, such as QQ, 360, Thunder, etc.;
  • Mobile APP, including Android APP, iOS APP;
  • CLI tools, that is, command-line programs without an interface.

After learning JavaScript, you are not only a full-stack engineer, but also omnipotent, which is brought to us by Node.js.

Chinese version of Node.js official website: https://nodejs.org/zh-cn/

1.1 What is runtime?

The so-called runtime refers to a series of components or tools that the program needs to rely on during operation; these tools and components are packaged together and provided to programmers, and programmers can run the code they write.
For JavaScript, it depends on the following components during runtime:

1) Interpreter
JavaScript is a scripting language that needs to be interpreted and run at the same time. The source codes used are compiled, and the whole process is completed by the interpreter. Without an interpreter, JavaScript is just a bunch of plain text files that cannot be read by a computer.
2) Standard library
We call some built-in functions in JavaScript code. These functions are not written by ourselves, but come with the standard library.
3) Local modules
The so-called local modules are modules that have been compiled in advance. They are binary files, and there is no difference in internal structure from executable files, but they cannot be run independently. These local modules are actually dynamic link libraries (.dll files under Windows). If you have used compiled languages ​​such as C and C++, you should be able to understand them better.

Many functions of JavaScript require the support of native modules , such as:

  • Cookie is used to store a small amount of user data. It is a small file on the user's computer. The use of Cookie must have the support of the file operation module.
  • Ajax can use the Internet to request data from the server, which is a network operation and must be supported by a network library.
  • Track the execution process of the code step by step to find logic errors. This process is called debugging and requires the support of a debugger (Debugger).
  • JavaScript can manipulate HTML, which requires the HTML parsing module to build a DOM tree in advance.

Local modules generally encapsulate common functions and have high performance requirements, so they are usually implemented using compiled languages, such as C language, C++, assembly language, etc.

The JavaScript interpreter needs the support of local modules, and the standard library will also call the interface of the local module when it is written, and the JavaScript code we write generally does not directly use the local modules, so the front-end web programmers cannot touch them.

The local module is the hero behind the scenes, it is not obvious, but it is indispensable.

Summary
Various components/tools such as interpreter, standard library, and local modules jointly support the running of JavaScript code, and they are collectively called JavaScript runtime.

2. Nodejs installation

cmd recommends using administrator mode to open

2.1 Installation environment

The computer installed this time is a 64-bit win10 system

Node.js download official website: https://nodejs.cn/download/
Enter the official website and click the download of the corresponding version directly
insert image description here

2.2 Installation steps

  1. Double-click the downloaded msi file and keep clicking Next
  2. When you arrive at the location of the configuration installation directory, you can click change to change to the installation location you want to specify, * (you can also use the default installation directory)
  3. Then keep clicking Next and wait for the installation to succeed

2.3. Check whether the installation is successful

  1. Press win+R, enter cmd, and open the command line
  2. Enter in the command line window, as shown in the figure below
    node -v displays the installed nodejs version
    npm -v displays the installed npm version
    insert image description here
  3. If the version number is displayed, the installation is successful

2.4 Modify the global module download path

I want to put the path of the whole module and the cache path in the folder where I installed node.js, then create two folders [node_global] and [node_cache] under the folder I installed, as shown in the figure below: after creating two
insert image description here
empty After the folder, press the [win+R] key on the keyboard, enter cmd, and press Enter to open the command line interface, and enter the following command.
npm config set prefix "E:\nodejs\nodejsLocation\node_global"

npm config set cache "E:\nodejs\nodejsLocation\node_cache"

Description:
prefix = the path of the created node_global folder
cache = the path of the created node_cache folder

Modify system environment variables
Change [Path] under [User Variables] to [E:\nodejs\nodejsLocation\node_global], and then click OK.
insert image description here
Under [System Variables], create a new [NODE_PATH] [E:\nodejs\nodejsLocation\node_global\node_modules]
insert image description here
under [System Variables] under [Path], add a new node global folder [E:\nodejs\nodejsLocation\node_global], and then Click OK.
insert image description here
After the above steps, the module downloaded by nodejs will be automatically downloaded to our custom directory, and then we will test it. Enter the following command:

npm install express -g # -g means global installation, without -g, it is downloaded to the current directory by default

As shown in the figure, the download is successful, and we return to the directory we defined to view.
insert image description here

It can be seen that the downloaded express module is successfully downloaded to the global specified directory.
insert image description here
Note: If the following error occurs when executing the command npm install express -g,
insert image description here
it is due to permissions. Right-click the Nodejs folder->Properties->Security, click Edit, and set all permissions to ✔.

2.5 Replace npm with Taobao image

Note: The default registry of npm means downloading npm packages from foreign servers, which is very slow in China, and usually points to Taobao https://registry.npm.taobao.org.

  1. Check out the initial npm source,npm config get registry
    insert image description here
  2. Replace with Taobao Mirror
    npm config set registry https://registry.npm.taobao.org/

3. Check whether the configuration is successfulnpm config get registry
insert image description here

2.6 Global installation of cnpm based on Taobao source

**Description:**Because the npm server is overseas, the access speed is relatively slow and the access is unstable. The cnpm server is provided by the Taobao team. In China, cnpm is an npm mirror image, which is generally updated synchronously, with a difference of 10 minutes, so cnpm will have an advantage when installing some software. But generally cnpm is only used for installation, so we still use npm for related operations such as project creation and uninstallation.

1. Globally install cnpm based on Taobao source
npm install -g cnpm --registry=https://registry.npm.taobao.org
insert image description here
insert image description here
2. After downloading, we can see the cnpm module locally
insert image description here
3. Execute the command to check whether cnpm is installed successfully
cnpm -v

As shown in the figure, it means that the cnpm environment configuration is successful.
insert image description here
Finally, thanks to the blogger – Peng Jiao

Reference (invasion and deletion):
What is Node.js? Introduction to Node.js
Nodejs Installation Tutorial

Guess you like

Origin blog.csdn.net/mfysss/article/details/129615059