Detailed Explanation of Node.js (3): Installation and Basic Use of Node.js

1. Node.js installation and configuration

Node.js supports installation and use on Windows, Linux and Mac

The installation is very simple, without complicated operations; I won’t go into details, just hang up a link for reference: https://www.runoob.com/nodejs/nodejs-install-setup.html

After the installation is complete, we can use the following command to view the current Node version:

$ node -v
v18.16.0
Note: There may be differences between different versions.

2. Introduction and use of nvm (node ​​version management tool is recommended)

1 Introduction

The full name of nvm is node.js version management. As the name implies, it is a nodejs version management tool. Through it, different versions of nodejs can be installed and switched.
In order to solve the incompatibility of various versions of node.
nvm is a tool that lets you install and switch between different versions of node on the same machine.
We may be working on two projects at the same time, and the node versions used by the two different projects are different, or we need to use a newer node version for experimentation and learning. In this case, it will be a very troublesome thing to maintain multiple versions of node, and nvm was created to solve this problem, it can easily switch between multiple node versions on the same device , and this is where the value of nvm lies. For details, please check the official website NVM official website.
Official website address: https://github.com/nvm-sh/nvm

2. Installation

two ways

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

At this point, nvm is installed under /.nvm.
Verify whether the installation is successful
Open the command line, and after executing the nvm -v command, a prompt will appear indicating that the installation is successful.

3. Basic installation/management of nodejs

##1、查看本地安装的所有版本;有可选参数available,显示所有可下载的版本。
nvm list [available]
nvm ls
##2、安装,命令中的版本号可自定义,具体参考命令1查询出来的列表
nvm install 11.13.0
##3、使用特定版本,切换node的版本,这个是全局的
nvm use 11.13.0
##4、卸载
nvm uninstall 11.13.0
##列出所有可以安装的node版本号
nvm ls-remote
##当前node版本
nvm current

4. Command Prompt

nvm arch :显示node是运行在32位还是64位。
nvm install <version> [arch] :安装node, version是特定版本也可以是最新稳定版本latest。可选参数arch指定安装32位还是64位版本,默认是系统位数。可以添加--insecure绕过远程服务器的SSL。
nvm list [available] :显示已安装的列表。可选参数available,显示可安装的所有版本。list可简化为ls。
nvm on :开启node.js版本管理。
nvm off :关闭node.js版本管理。
nvm proxy [url] :设置下载代理。不加可选参数url,显示当前代理。将url设置为none则移除代理。
nvm node_mirror [url] :设置node镜像。默认是https://nodejs.org/dist/。如果不写url,则使用默认url。设置后可至安装目录settings.txt文件查看,也可直接在该文件操作。
nvm npm_mirror [url] :设置npm镜像。https://github.com/npm/cli/archive/。如果不写url,则使用默认url。设置后可至安装目录settings.txt文件查看,也可直接在该文件操作。
nvm uninstall <version> :卸载指定版本node。
nvm use [version] [arch] :使用制定版本node。可指定32/64位。
nvm root [path] :设置存储不同版本node的目录。如果未设置,默认使用当前目录。
nvm version :显示nvm版本。version可简化为v。

Please add a picture description

3. The first Node.js program: Hello World!

script mode

Here is our first Node.js program:

example

console.log(“Hello World”);

Save the file, the file name is helloworld.js, and execute it through the node command:

node helloworld.js

After the program is executed, if it is normal, it will output Hello World on the terminal.

interactive mode

Open the terminal, type node to enter the command interaction mode, you can enter a code statement and execute it immediately and display the result, for example:

$ node
> console.log('Hello World!');
Hello World!

Create the first application with Node.js
If we use PHP to write the back-end code, we need Apache or Nginx HTTP server, and it is equipped with mod_php5 module and php-cgi.

From this point of view, the entire requirement of "receiving HTTP requests and providing Web pages" does not need to be handled by PHP.

But for Node.js, the concept is completely different. When using Node.js, we're not just implementing an application, but an entire HTTP server. In fact, our web application and corresponding web server are basically the same.

Before we create the first Node.js "Hello, World!" application, let's understand what parts a Node.js application consists of:

  1. Introducing required modules: We can use the require command to load Node.js modules.
  2. Create a server: The server can monitor client requests, similar to HTTP servers such as Apache and Nginx.
  3. Receiving requests and responding to requests The server is easy to create, the client can use a browser or terminal to send HTTP requests, and the server returns response data after receiving the request.

Create a Node.js application

Step 1. Import the required module.
We use the require command to load the http module, and assign the instantiated HTTP to the variable http. The example is as follows:

var http = require("http");

Step 2. Create a server
Next, we use the http.createServer() method to create a server, and use the listen method to bind port 8888. The function receives and responds to data through request and response parameters.

The example is as follows, create a file called server.js in the root directory of your project, and write the following code:

var http = require('http');

http.createServer(function (request, response) {
    
    

    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {
    
    'Content-Type': 'text/plain'});

    // 发送响应数据 "Hello World"
    response.end('Hello World\n');
}).listen(8888);

// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

With the above code we have completed a working HTTP server.

Use the node command to execute the above code:

node server.js
Server running at http://127.0.0.1:8888/

img

Next, open your browser and visit http://127.0.0.1:8888/, you will see a web page that says "Hello World".

img

Analyzing Node.js' HTTP server:

The first line requires the http module that comes with Node.js and assigns it to the http variable.
Next we call a function provided by the http module: createServer . This function returns an object that has a method called listen that takes a numeric parameter specifying the port number that this HTTP server is listening on.

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/131010762