The article directories

  • 2.1. Install the Node environment · Check the version of the current Node environment
  • 2.2. Hello World
      • Case 1: Create a file named 00-helloworld.js and write
      • Case 2: No DOM and BOM
      • Case 3: Reading a file
      • Case 4: Write a document
      • Case 5: Reading a file that does not exist
      • Plus misjudgment
      • Failed to write file
      • File writing succeeded

2.1. Install the Node environment · Check the version of the current Node environment

  • Download: nodejs.org/en/download…

  • The installation

    • Fool’s waynextIt is ok
    • For those that have already been installed, reinstalling will upgrade
  • Check whether the Node environment is installed successfully

Node -v or node --versionCopy the code

  • The environment variable

2.2. Hello World

  1. Create write JavaScript script file
  2. Open the terminal and locate the directory where the script file resides
  3. Enter the node file name to execute the corresponding file

Note: Do not name the file using node.js, otherwise executing the node file name will open the file. This means you can use whatever name you like except node, and preferably not in Chinese.

Case 1: Create a file named 00-helloworld.js and write

var foo = 'hello nodejs'

console.log(foo)
Copy the code
  • incmdWindow output orgitbashOr vscode’s own terminal
node 00-helloworld.js
Copy the code

Case 2: No DOM and BOM

  • In Node, EcmaScript is used for encoding
  • No BOM, NO DOM
  • It’s not like JavaScript in a browser
console.log(window)
console.log(document)
Copy the code

  • The error message is similar to the error message in the browser background (F12) : reference error, Window is not defined
  • There is no DOM or BOM in node
  • Parsing executing JavaScript
  • Read and write files
  • http

Case 3: Reading a file

  • JavaScript in browsers does not have file-manipulation capabilities
  • But JavaScript in Node has file-manipulation capabilities
  • Fs isfile-systemFile system is short for file system
  • If you want to perform file operations in Node, you must importfsThis core module
  • infsIn this core module, all the relevant file operations are providedAPI
  • Such as:fs.readFileIt’s used to read files
  1. Load the FS core module using the require method
var fs = require('fs')
Copy the code
  1. Read the file

    • The first parameter is the path to the file to read

    • The second argument is a callback function

      • successful

        • The data of data
        • error null
      • failure

        • Data undefined Has no data
        • Error Error object
  • error

    If the read fails,errorThat’s the error object

    If the read succeeds,errorisnull
  • data

    If the read succeeds,dataThat’s the data that you read

    If the read fails,errorThat’s the error object
fs.readFile('./data/hello.txt'.function (error, data) {
	console.log(data)
})
Copy the code
  • In the newhello.txtWrite to file
Hello nodejs hello Node.jsCopy the code
  • performNode 02- Reads files
var fs = require('fs')
fs.readFile('./data/hello.txt'.function (error, data) {
	console.log(data)
})
Copy the code

<Buffer 68 65 6c 6c 6f 20 6e 6f 64 65 6a 73 0d 0a 0d 0a>
Copy the code
  • The files are actually storing binary data, 0, 1
  • Why don’t we see zeros and ones here? The reason is binary has been converted to hexadecimal
  • But neither binary 01 nor hexadecimal is known to humans
  • So we can go throughtoStringMethod to convert it to a character we can recognize
var fs = require('fs')
fs.readFile('./data/hello.txt'.function (error, data) {
	console.log(data.toString())
})
Copy the code
  • performNode 02- Reads files

Case 4: Write a document

  • The first parameter: the file path

  • The second parameter: file content

  • The third argument: the callback function error. Only one function is accepted here. Error is a parameter and can be called by any other name

    • Success:

      • File writing succeeded
      • The error is null
    • Failure:

      • File write failure
      • An error is an error object
var fs = require('fs')

fs.writeFile('. / data/hello. Md '.'Hi, I'd like to introduce you, I'm Node.js'.function (error) {
  console.log('File write succeeded')})Copy the code

Pay attention to: can be invscodeRight-click the file name to open the terminal location or shortcut keyCtrl+Alt+O

  • Perform:

  • At this point you can see that this will be created directly after executionHello, md,File and write content

Case 5: Reading a file that does not exist

  • a.txtHas not been created
var fs = require('fs')
fs.readFile('./data/a.txt'.function (error, data) {})Copy the code
  • No prompt is displayed after execution

  • Already createdhello.txtTo check for errors
var fs = require('fs')
fs.readFile('./data/hello.txt'.function (error, data) {
	console.log(error)
})
Copy the code
  • If yes, the data is successfully read

  • Make it nonexistenta.txt
var fs = require('fs')
fs.readFile('./data/a.txt'.function (error, data) {
	console.log(error)
})
Copy the code
  • If the command is executed, the read fails



Error is an error object, and data is real data

var fs = require('fs')
fs.readFile('./data/a.txt'.function (error, data) {
	console.log(error)
	console.log(data)

	console.log(data.toString())
})
Copy the code

Undefined has no toString attribute

  • successful

    • The data of data
    • error null
  • failure

    • Data undefined Has no data
    • Error Error object

Plus misjudgment

  • a.txtThis is where you can check if an error has occurred by checking for error
var fs = require('fs')
fs.readFile('./data/a.txt'.function (error, data) {
	 if (error) {
    console.log('File reading failed')
    // return
  		} else {
    console.log(data.toString())
  	}
})
Copy the code

Failed to write file

  • Now the file name is? .txtBecause special characters cannot be used as file names in Windows
var fs = require('fs')
fs.writeFile('./data/? .txt'.'Hi, I'd like to introduce you, I'm Node.js'.function (error) {
   console.log(error)   // Check for errors
Copy the code

File writing succeeded

var fs = require('fs')
fs.writeFile('. / data/hello. TXT '.'Hi, I'd like to introduce you, I'm Node.js'.function (error) {
   console.log(error)   // Check for errors
Copy the code



error = nullThe file is successfully written

  • And judgment
var fs = require('fs')
fs.writeFile('. / data/hello. TXT '.'Hi, I'd like to introduce you, I'm Node.js'.function (error) {

   if (error) {
    console.log('Write failed')}else {
    console.log('Write succeeded')}Copy the code