Home  >  Article  >  Web Front-end  >  Quickly master the installation and operation method of Node.js environment_node.js

Quickly master the installation and operation method of Node.js environment_node.js

WBOY
WBOYOriginal
2016-05-16 15:15:251386browse

Installer
NodeJS provides some installers, which can be downloaded and installed at nodejs.org.

Under Windows system, select the installation file with .msi suffix that matches the system version. Under Mac OS X system, select the installation file with .pkg suffix.

Compile and install
There is no ready-made installation program available under Linux system. Although some distributions can be installed using methods such as apt-get, they may not be able to install the latest version. Therefore, the following compilation method is generally used to install NodeJS under Linux systems.

1. Make sure that the g++ version in the system is above 4.6 and the python version is above 2.6.

2. Download the latest version of NodeJS source code package with tar.gz suffix from nodejs.org and extract it to a certain location.

3. Enter the unzipped directory and use the following commands to compile and install.

$ ./configure

$ make

$ sudo make install

Run

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

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

If you want to run a large section of code, you can write a JS file first and then run it. For example, there is the following hello.js.

function hello() {
  console.log('Hello World!');
}
hello();

After writing, type node hello.js in the terminal to run. The results are as follows:

$ node hello.js
Hello World!

Permission issue
Under Linux systems, root privileges are required when using NodeJS to listen on port 80 or 443 to provide HTTP(S) services. There are two ways to do this.

One way is to use the sudo command to run NodeJS. For example, server.js run through the following command has permission to use ports 80 and 443. This method is generally recommended to ensure that root permissions are only provided to JS scripts that need it.

$ sudo node server.js

Another way is to use the chmod +s command to make NodeJS always run with root privileges. The specific method is as follows. Because this method allows any JS script to have root permissions, it is not very safe, so it is not recommended for systems that require high security considerations.

$ sudo chown root /usr/local/bin/node
$ sudo chmod +s /usr/local/bin/node

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn