Node.js installation and basic use

Posted by sugar on Fri, 26 Nov 2021 03:44:00 +0100

1, Install Node.js

Node.js installation method

2, Node.js function

  1. Node can be used as a front-end tool, which can be installed through NPM.
  2. Node can develop some front-end tools.
  3. Node can be used as a server.
  4. It can operate the database and provide external interfaces.
  5. Can realize instant chat program.
  6. You can make some useful tools, such as editors.

3, Image configuration of Node.js

**Configure Taobao image * * because each installation package needs to go to the foreign network, and the speed is very slow, Taobao helped us create an NPM package hosting website in China, which can improve the speed of NPM package installation!
npm config set registry --global
npm config set disturl --global

Download Kit

  • The package manager npm of Node.js is the largest open source library ecosystem in the world.
  • First, use npm init -y to initialize the package management file package.json. In the future, all installed packages will be recorded in this file
  • Use the npm install package name -- save / -- save dev to install the package; Among them, install can be abbreviated as i:
  • --save can be abbreviated as - S-- save dev can be abbreviated as - D;
  • npm uninstall package name -- save / -- save dev or npm remove package name -- save / -- save dev;
  • npm i webpack -g, where, - g indicates that some packages are installed globally. The packages installed through - g are under C:\Users \ user name \ AppData\Roaming\npm.
  • Configure the domestic Taobao image of yarn
yarn config set registry --global
yarn config set disturl --global

4, Two ways to execute js code using Node

  1. Directly enter node in the command letter to REPL the operating environment of node:
R: Read: Read the string content entered by the user
E: Evaluate: Take the string entered by the user as JS Code parsing and execution
P: Print: Print input Evaluate Analytical results
L: Loop: Enter the next cycle
  1. Write the node code into a JS file, and then run the node code through the JS file path to be executed by node.

5, fs of built-in modules in Node

  1. fs module is one of the core built-in modules in Node.js.
  2. It is mainly used to read and write files.

Read file data

// The fs module will exist directly after the node has been installed. It can be used directly, but it must be referenced first
// Core API (module): Node official found that some modules were used very frequently, so they compiled these frequently used modules into binary files
// Put it into the installation package of Node. If Node is installed, the core modules inside can be used directly
// require indicates how to introduce the core module
// 1. Introduction module
var fs = require('fs');

// 2. Call the method to read the file
	if (err) return console.log(err.message);

 * readFile Method parameters
 * 1. The first parameter is the path to read the file 
 * 2. Coding method
 * 3. Callback function after reading operation 

Write data to file

// 1. Introduce core modules
var fs = require('fs');

// 2. Call the method of writing data to write data
 * The write method has four parameters:
 * 1. Parameter 1 is the path to write to the file
 * 2. Parameter 2 is the data to be written
 * 3. Parameter 3 is optional. It is the encoding method for writing data. The default is UTF-8
 * 4. Parameter 4 is the callback function after writing the file, with an err parameter
 *    If the write succeeds, err is null. If the write fails, err is a specific object
 * be careful:
 * 1. If the file written does not exist, the file will be created before the content is written
 * 2. If the file already exists, the newly written content will overwrite the original content
 * 3. If you want to append something later, you can use the method appendFile
fs.writeFile('./files/2.txt','study hard,make progress every day',(err)=>{
  if(err) throw err;
  console.log('Write successful');


// Import package (fs is also built-in)
const fs = require('fs');
const path = require('path');
// Read the file using the methods in the package 
// fs.readFile();   Read file asynchronously
// fs.readFileSync();  Read file synchronization is rarely used

// let data = fs.readFileSync('./test01.txt', { encoding: 'utf-8' });
// console.log(data);

// let url  = path.join(__dirname)
let url = path.join(__dirname,'test01.txt');

fs.readFile(path.join(__dirname,'test01.txt'), { encoding: 'utf-8' }, (err, data) => {
    if (err) throw new Error(err.message);

// console.log(__dirname);

// fs.writeFile('./1.txt','js is hard to learn ', (err, data) = >{
//     console.log(err);
// })

6, Arrow function

  1. The arrow function is an ordinary anonymous function, which solves the problem of this pointing.
  2. This inside the arrow function is always consistent with this in the external scope.
  3. The writing method of standard arrow function: () = > {}.

Variant of arrow function:
Variant 1

// If there is only one parameter in the parameter list, you can omit the parentheses and write the parameter directly
// If there are no parameters on the left, or there are multiple parameters, the parentheses cannot be omitted

Variant II

// If there is only one line of code in the function, {} outside the function body can be omitted, but it is generally not omitted
(a,b)=> console.log(123)

7, __dirname

  1. When Node reads and writes files, the path is to find files according to the path of the folder where the Node command is run.
  2. In order to solve the path problem of fs module in Node, a _dirname method is provided in Node.
  3. __dirname always gets the root directory of the currently executing js file, which is an absolute force, starting from the system drive letter.
  4. This path is dynamically variable. The path is also different according to the execution file, but it must be an absolute path, starting with the drive letter.
// 1. Introduction module
var fs = require('fs')

// 2. Call the method to read the file
// When Node reads and writes files, the path is to find the file according to the path of the folder where the Node command is run
// In order to solve the path problem of the fs module in the Node, the Node provides a _dirname, which is an absolute path
// __dirname always gets the absolute path where the current js file is located
// fs.readFile('./files/2.txt','utf-8',(err,data)=>{
  if(err) throw err;


Topics: Javascript Front-end