What is 1-Node.js?
A JavaScript runtime environment based on chrome V8 engine, it is not a programming language, but provides an environment for us to run JS code
2- Download and install paths
Download:nodejs.org/en/
Chinese website:Nodejs.cn/
Installation: Double-click the installation file to start the installation (select the corresponding installation file for different systems)
You are advised to use an English installation directory (not d:/ software /node). After the installation is complete, it will not display a shortcut icon on the desktop to test whether the installation is successful. Run CMD and enter node -v to check whether the installation is successful
3- How to run JS code?
Write a js file, open the command line tool, the path is best located in this JS file directory
Run the node file path (file name) command to automatically complete the file using the shortcut key TAB
4- Differences between Node.Js and the browser
Similarities: Both support ECMAScript(variables, operations, functions)
Differences: Node.js has no window object, BOM, or DOM
There are no Node.js modules in the browser
5- Command line commands and shortcut keys
Clear the screen: CLS
Display the contents of the current folder :dir/ls
Go to the root directory of the current disk: CD \
Go to the upper directory: CD..
Go to the specified directory: CD target name (level by level)
Stop the program: CTRL + C
Automatic completion: TAB
Bring up the history command: up and down arrows
Copy and paste: right mouse key
6-Node.js module classification
01- Core Templates (Built in)
02- Custom templates (we brought our own)
03- Third Party Modules (written by others)
07- With all the basics covered, now let’s look at the first module
Fs module
7.1- The core module comes with Node and its name is fixed and cannot be written arbitrarily.
Const fs = require("fs") // Check consolo.log(fs) // call fs.xxxxxxCopy the code
7.2- Reading files :readFile()
// introduce: const fs = require("fs"); ReadFile (' file path (filename),' string encoding ', callback function){} // For example, 1.fs. readFile(' filename ','utf8),function(err,data){// Handle errors and proceed If (err) {return the console. The log (err)} the console. The log (data)}) / / as case 2: do not add a character encoding / / do not add a character encoding, are automatically converted into a binary or hexadecimal, so we want to use the toString () Fs.readfile (' file name ',function(err,data){if(err) return console.log(err) console.log(data.tostring ())}) // Synchronization is completed before asynchro is performed Const fs = require('fs') // Fs.readfile (' file name ', character encoding) try{}catch(){} Let content = fs.readFile(' file name ',uft8); Console. log('content')} Catch (err){// Error handling console.log(err)}Copy the code
7.3 -fs.writefile () overwrites writes
Const fs = require("fs") // call // the type passed to writeFile must be a string // fs.writeFile(' filename ',' overwrite ',' character code ', callback function) // Let arr = [{name: 'brother'}, {age: 18}]; // Convert to JSON string format let strarr = json.stringify (arr); Fs.writefile ('02-.txt', strarr, (err) => {if (err) {console.log(' error ', err); return; }})Copy the code
7.4-fs.appendFile() Appended data
const fs = require('fs'); / / call / / syntax format: appendFile (" file path ", "additional content", "character encoding, the callback function) {} / / \ n is wrapping the fs. AppendFile (' 02 -. TXT ', '\ n the hello, function (err) { if (err) { return console.log(err); }})Copy the code