This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

As a front end kid, how can you only know the front end stuff? There will be a Nodejs tutorial series coming up.

1. Summary of Nodejs

1.1 introduce Nodejs

  • Nodejs is an ECMAScript runtime environment based on the Chrome V8 engine.
  • Nodejs can execute js files (with Nodejs, js files can run on the server).
  • Nodejs provides a number of tools (apis) that allow you to read and write files, create Web servers, and more.

1.2 installation Nodejs

Liverpoolfc.tv: nodejs.org/en/

Chinese: nodejs.cn/

LTS: Long term stable version. The actual project development recommends using the long-term stable version

Current: Current version. The latest version contains some new features, and you can use it if you want to learn the latest features. However, the latest version may have some unknown bugs.

Download and install a foolproof Next installation. After the installation is complete, open CMD and enter node -v to view the node version

1.3 Differences between Nodejs and browser JS

Js components in the browser: ES core + DOM + BOM

Nodejs js components: ES core + global member + module system (system module, third-party module, automatic sense module)

Global members: setTimeout, setInterval, console.log(), etc. Note that these names and functions are the same as in the browser, but are implemented by NodeJS itself, browser-independent.

1.4 Program Experience

The running results are as follows:

2. Command lines and common commands

2.1 Introduction to cli

  • Before Windows was released, people used DOS (disk operating system) to operate computers.
  • The DOS system has many built-in commands (for example: CD, dir, etc.). We can operate the computer by calling different commands.
  • After Windows was released, the command line operation mode was retained, and there were two built-in command lines: Cmd and PowerShell. We could use the command line to operate the computer. Windows 7 defaults to Cmd and Windows 10 defaults to PowerShell.

2.2 Common Commands

  • CD: Switch the directory
  • Dir: View all files and directories in the directory
  • CLS: CLS
  • Exit, exit
The command meaning
cd \ Switch to theThe root directory
cd . Stay in the current directory
cd .. Switch to theThe higher the directory
CD [directory] Switch to the specified directory
dir View all files and directories in the current directory
CD [directory] View all files and directories in the specified directory
tip
  • Auto completion: You can press TAB to complete the unfinished commands
  • History command: Press ↑ or ↓ to find the previous command

3. Nodejs system module

3.1 System Modules

  • System module: A module installed with Nodejs and maintained by officials.

    • Common system modules: FS, HTTP, URL, path, etc
  • A module contains many methods and properties that help us implement different functions

3.2 File System Module – fs

It is used to manipulate files and directories. Can create/delete files, read files, obtain file details and other operations

3.3 File reading — readFile

const fs = require('fs');
fs.readFile(var1, var2, var3);
/** Parameter 1: specifies the file path to be read. The absolute path can be either a relative path or an absolute path. Parameter 2: specifies the configuration item, which is used to configure the character set. This parameter is optional. If this parameter is not set, the file content is returned in binary form. Parameter 3: Callback function triggered after the file is read successfully. It has two parameters, err and result: error: null result: file content. You can use the toString() method to convert binary data to a normal string. Read failure: err: error object result: undefined */
Copy the code

3.4 File Writing: writeFile

// Target: write a string to the b. TB file in the files directory

//1. Import/load/import fs module
const fs = require('fs');

//2. Call the writeFile method to write content to the file
// The writeFile method has two features:
// 1) If the file is not available, try to create it
// 2) writeFile is overwrite write
/** * Parameter 1: the path of the file to be written, both absolute and relative * Parameter 2: the string to be written * Parameter 3: the character set, which defaults to UTF-8 * Parameter 4: the callback function that is triggered when the write is complete. This function has an err */ parameter

fs.writeFile('./files/b.txt'.'Kazak, face the wind.'.'utf-8'.(err) = > {
    if (err) {
        console.log(err); }})Copy the code

3.5 appendFile — appendFile

// Target: append a string to the b.tb file

//1. Import/load/import fs module
const fs = require('fs');

//2. Call appendFile to append content to the file
/ / features:
// 1) If the file is not available, try to create it
// 2) Append data
// 3) The parameters are exactly the same as the writeFile method

/** * Parameter 1: the path of the file to be appented * Parameter 2: the string to be appented * Parameter 3: the character set, which defaults to UTF-8 * Parameter 4: the callback function that is triggered when the write is complete, with an err */
fs.appendFile('./files/b.txt'.'-- I'm Yasso. '.(err) = > {
    if (err) {
        console.log(err); }})Copy the code

3.6 Traversing the directory – readdir

// Target: get the names of all files and folders in D:\***\ \WWW directory

//1. Load fs module
const fs = require('fs');

//2. Call the readdir method to traverse the directory
/** * Parameter 1: the path of the directory to be traversed * Parameter 2: the character set, which is utF-8 by default * Parameter 3: the callback function to be triggered after the traversal is complete. There are two parameters, err and files * err: error object * files: the names of all files and directories in an array */
fs.readdir('D:/***/***/WWW'.(err, files) = > {
    if (err) {
        return console.log(err);
    }

    console.log(files);
})
Copy the code

Praise support, hand stay fragrance, and have glory yan, move your small hand to make a fortune yo, thank you for leaving your footprints.

The next part focuses on Node creating a Web server

4. 4. Smart Car

Front ten thousand literal classics – basic

Front swastika area – advanced chapter

Let’s talk about two of the most common administrative tools used in front-end development

Talk about regular expressions