fs module
fs module is a core module of Node.js, which is specially used for files in the operating system
Fs.readfile (file path, options, callback function)
Fs.readfilesync (first parameter, second parameter)
- The first parameter is the file path
- The second parameter can be an object representing the configuration or a string representing the encoding of a text file. The default configuration object is {encoding: null, flag: 'r'}, that is, the file encoding is null by default, and the reading mode is r (read-only) by default. If the second parameter does not specify encoding, the readFileSync method returns a Buffer instance, otherwise it returns a string
Fs.writefile (file path, written data, options, callback function)
- Asynchronously writes the specified data to the file with the specified path. After writing the specified data, the callback function triggers asynchronous writing to the file. If the file does not exist, it will be created automatically. Any data type can be written in the file, and all data will be implicitly converted to a string.
Fs.writefilesync (file path, written data, options) - Synchronously writes the specified data to the file of the specified path
Fs.existssync (file path) determines whether the path exists
Fs.stat (file path) view the information of the specified file
Fs.truncate (file path, byte length, callback function) limits the file size to the specified bytes
Fs.unlink (file path, callback function) deletes the file with the specified path
Fs.rmdir (file path, callback function) deletes the empty folder of the specified path
Fs.mkdir (file path, callback function) creates an empty folder with the specified path
Fs.readdir (file path, callback function) reads all files in the specified folder
fs.rename(oldPath,newPath, callback path) moves the oldPath file to newPath.
- Rename (old path, new path, callback function) 1. Move the file of the old path to the new path 2. And rename it
qs module
const qs=require('querystring')
qs.parse() parses the URL into the form of an object
qs.stringify() is to sequence objects into URL s for splicing (a = Zhang San & B = Li Si)
// qs.parse() parses the URL into the form of an object let url = 'method=data&projectId=10086&appToken=12345; qs.parse(url) Output is: { method:'data', projectId:'10086', appToken:' 12345' } // qs.stringify() is to serialize objects into URL s for splicing (a = 1 & B = 2) let url = { method:'data', projectId:'10000', appToken:'54321' } qs.stringify(url) Output is: 'method=data&projectId=10000&appToken=54321';
http module
// The qs module provides utilities for parsing and formatting URL query strings. You can access it in the following ways: // The qs.parse() method parses the URL query string str into a collection of key value pairs. //1. const http = require('http'); const querystring = require('querystring'); const qs = require("querystring"); //2. const server = http.createServer((request, response)=>{ //Extract the user name and password from the form //1. Declare a variable let body = ''; //2. Bind data event request.on('data', (chunk) => { //Each read is 64kb, and multiple reads are required body += chunk; //chunk is binary data, and adding it to a string is also a string }); //3. Bind end event request.on('end', ()=>{ // console.log(body); // Convert string to object const data = qs.parse(body); // Read out all user information const users = fs.readFileSync('./users.json').toString(); // Convert string to object const usersObj = JSON.parse(users); // Press the object of the new user into the usersObj object usersObj.data.push(data); // Convert object to JSON string let str = JSON.stringify(usersObj); fs.writeFileSync('./users.json', str + `\r\n`); console.log(body); response.end("over"); }); }); //3. server.listen(8000, ()=>{ console.log('Service started, 8000 Port listening.....'); })