This is the 9th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021.

Nodejs, Nodejs, Nodejs, Nodejs, Nodejs, Nodejs

1. Fs module

Import fs template:

const fs = require("fs")
Copy the code

When you load other modules using require(), the code in the loaded module is executed

The fs.readFile() method is used to read the contents of the specified file

  • Grammar:

    fs.readFile(path[,options],callback(err,datastr){})
    Copy the code

Path: indicates the file path

Ptions: Optional, which encoding format is used to read the file

Allback: after fetching, get the result of reading through the callback function

Err: Indicates the result of reading failure

Datastr: Indicates that the data is successfully read

const fs = require('fs'); fs.readFile('./day1/content.txt','utf8',function(err, dataStr){ console.log(err); / / read the success, errw console. The log (" -- -- -- -- -- -- -- -- -- -- -- -- ") to the console. The log (dataStr); })Copy the code

The fs.writefile () method is used to write to the specified file

  • Grammar:

    fs.writeFile(file, data[, options], callback(err){})
    Copy the code

    File: indicates the file storage path

    Data: indicates the written content

    Options: format for writing file contents

    Callback: callback function,err parameter, failed to write file,err is null

    const fs = require('fs'); fs.writeFile('./day1/content.txt', 'new Content ','utf8',function(err){if(err){return alert(" write file failed ")} return(" write file failed ")})Copy the code

    WriteFile () overwrites the contents of previous files

    __dirname indicates the directory where the current file resides

2. Path Path module

Role: A module used to process paths

  • The path.join() method is used to concatenate multiple path fragments into a complete path string

    • path.join(__dirname, ‘./day1/content.txt’)
  • The path.basename() method is used to parse the filename out of the path string

    • Path. basename(path[,etc]), return the last part of the path
  • Path.extname (path), obtain the extension name in the path

3. HTTP module

Purpose: Create a Web server module.

  • http.createServer()Method to create a Web server instance
  • req.url
  • req.method
  • res.setHeader(‘Content-Type’, ‘text/html; Charset = utF-8 ‘)// Prevent garbled characters in Chinese
  • res.end(str)// Send the content containing Chinese to the server
Const HTTP = require(" HTTP ") // create a web server instance const server = http.createserver () server.on('request',function(req, res){ console.log("Someone visit our server") }) server.listen(80, Function (){console.log("server running at http://127.0.0.1:8080")})Copy the code

4. Loading mechanism of modules

  1. Loading from cache is preferred

    Modules are cached after the first load, and multiple calls to require() will not cause the module’s code to be executed more than once. Nodejs modules are preferentially loaded from the cache to improve module loading efficiency.

  2. The built-in modules have the highest loading priority

  3. When using require() to load a custom module, you must specify./ or.. Path identifier starting with /. If not, Node loads it either as a built-in module or as a third-party module.

Well, that’s all for today. Keep up the good work!