Install Node.js on MacOS

Table of contents

1. Introduction to Nodejs

2. Installation method

1.Official website download

2.Installation

3. Post-installation verification

1. Command verification

2.Web service verification

4. The relationship between Node.js and npm


1. Introduction to Nodejs

Node.js is essentially a JavaScript runtime environment that provides the ability to run JavaScript code on the server side. Node.js has the following advantages:

1. High performance: Built on the Chrome V8 engine, it has fast execution speed and optimized memory management. It also uses an event-driven non-blocking asynchronous I/O model, which can more effectively utilize server resources and provide better performance.

2. Cross-platform: Can run on multiple operating systems, including Windows, MacOS and Linux. Enables developers to easily write code once and then deploy and run it on different platforms.

3. A large number of module and tool support: With a rich module ecosystem, modules and tools can be easily installed, used and shared through the npm package manager, which enriches the functions of Node.js and can help developers improve development efficiency and code quality.

4. Scalability and flexibility: It has good scalability and can easily build a distributed system or microservice architecture. It also supports building custom HTTP servers and WebSocket servers, providing more flexible development options.

2. Installation method

1.Official website download

 Official website link: https://nodejs.org/en/, click on the stable version in the red box to download and install.

2.Installation

Double-click the downloaded file to pop up the installation interface as shown below. When installing Node.js, the package manager npm will be installed together. Click to continue the installation all the way down.

The installation end page is installed to the /usr/local/bin path by default.

3. Post-installation verification

1. Command verification

After the installation is complete, open the terminal and enter the following command:

node -v
npm -v
The version information appears as shown below, indicating that the installation is successful.

2.Web service verification

Create a new node-server.js file, enter the following code and save it.

var http = require("http");

http.createServer(function(request, response) {
  console.log(request.method);
  console.log(request.url);
  console.log(request.headers);
  response.writeHead(200, {
    "Content-Type" : "text/plain"
  });
  response.write("Welcome to Nodejs");
  response.end();
}).listen(8000, "127.0.0.1");

console.log("Create server on http://127.0.0.1:8000/");

Open the terminal and enter the directory where node_server.js is located, enter node node-server, and print out the log to indicate that the web startup is successful.

After starting, Nodejs will listen for requests on the local port 8000. Open the browser and enter http://127.0.0.1:8000 in the address bar. If the page returns "Welcome to Nodejs", it means that the Web Server is running successfully. It is simple enough.

4. The relationship between Node.js and npm

npm is the official package manager for Node.js, providing a convenient way to manage the modules and dependencies required by Node.js applications. They are usually used together. By creating a package.json file in the project directory, listing the required module dependencies in it, and then executing the npm install command, npm will Download the required modules to the project's node_modules directory.

Guess you like

Origin blog.csdn.net/BlogPan/article/details/131964479