Linux system deployment Node.js environment
1. Introduction to Node.js
1.1 Introduction to Node.js
1. Node.js is an open source and cross-platform JavaScript runtime environment.
2. Node.js is a JavaScript runtime based on the Chrome V8 engine, a platform for server-side programming that can parse and execute JavaScript code.
3. Node.js can handle a large number of concurrent requests, and can easily build high-performance network applications.
4. Node.js can also use the npm package manager to easily manage dependencies and modules.
1.2 Introduction to npm
1.npm is a package management tool for Node.js, which is used to install, publish, share and manage code packages.
2. Node.js is an open source backend service technology that runs JavaScript. npm is a key part of the Node.js community, which enables developers to easily install and update code bases, as well as manage dependencies between different software packages.
3. npm also provides a global code sharing platform that enables developers to access a large number of software packages and resources, thus providing convenience and flexibility for the development process.
1.3 Node.js official website
- Node.js official website address: https://nodejs.org/en
- Node.js software package download address: https://nodejs.org/dist/
2. Introduction to the local environment
2.1 Local Environment Planning
This practice is a personal test environment, and the operating system version is centos7.6.
hostname | IP address | operating system version | kernel version | Node.js version |
---|---|---|---|---|
jeven | 192.168.3.166 | hundred 7.6 | 3.10.0-957.el7.x86_64 | v14.17.0 |
2.2 Introduction to this practice
1. The deployment environment for this practice is a personal test environment;
2. The Node.js environment is deployed in the centos7.6 environment.
3. Deploy the Node.js environment
3.1 Download the Node.js installation package
Use the following command to download the Node.js installation package.
wget https://nodejs.org/dist/v14.17.0/node-v14.17.0-linux-x64.tar.xz
3.2 Unzip the Node.js installation package
Unzip the Node.js installation package
tar -xvJf node-v14.17.0-linux-x64.tar.xz
3.3 Copying binaries
Copy the binary files under /root/node-v14.17.0-linux-x64/bin/ to /usr/local/bin
cp -a /root/node-v14.17.0-linux-x64/bin/node /usr/local/bin/node
cp -a /root/node-v14.17.0-linux-x64/bin/npm /usr/local/bin/npm
3.4 Configure environment variables
- In the /etc/profile file, add the following two lines:
export NODE_HOME=/root/node-v14.17.0-linux-x64/bin/
export PATH=$PATH:$NODE_HOME:/usr/local/bin/
- Make the variable file take effect
source /etc/profile
- You can also use the method of creating soft links, so that you can directly use node and npm commands in any directory
ln -s /root/node-v14.17.0-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v14.17.0-linux-x64/bin/npm /usr/local/bin/npm
3.5 Check node and npm version
Check node and npm version
[root@jeven Ghost]# npm -v
6.14.13
[root@jeven Ghost]# node -v
v14.17.0
4. NPM testing work
4.1 Create a new directory
Create a new test directory
mkdir -p /data/npm/
4.2 NPM initialization
npm init
[root@jeven npm]# npm init -y
Wrote to /data/npm/package.json:
{
"name": "npm",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
4.3 Install the test module package
Install the test module package, mocha is a test framework, and chai is an assertion library.
npm install mocha chai --save-dev
4.4 Create a test file test.js
Create a test file test.js
[root@jeven npm]# cat test.js
var assert = require('chai').assert;
describe('测试', function() {
it('1 等于 1', function() {
assert.equal(1, 1);
});
});
4.5 Running a test
If the test passes, a result similar to the following will be output, and if the test fails, an error message will be output.
[root@jeven npm]# ./node_modules/mocha/bin/_mocha test.js
测试
✔ 1 等于 1
1 passing (4ms)
5. Deploy the test project
5.1 Edit test.js file
Edit edit test.js test file
[root@jeven ~]# cat test.js
const http = require('http');
const hostname = '0.0.0.0';
const port = 5000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Learning makes me happy\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${
hostname}:${
port}/`);
});
5.2 Running the test project
run test project
[root@jeven ~]# node ~/test.js &
[1] 24625
[root@jeven ~]# Server running at http://0.0.0.0:5000/
- View background running tasks
[root@jeven ~]# jobs
[1]+ Running node ~/test.js &
5.3 Check the listening port
Check the listening port 5000
[root@jeven ~]# ss -tunlp |grep 5000
tcp LISTEN 0 128 *:5000 *:* users:(("node",pid=26742,fd=18))
5.4 Accessing test items
In the local browser, visit: http://192.168.3.166:5000/