Common usage instructions of built in modules fs and path in nodejs

  • 2021-09-12 00:27:56
  • OfStack

readFile

readFileSync Read Data Synchronously

var fs=require('fs');

fs.readFileSync('./a.txt');

readFile Asynchronous Data Read


var fs=require('fs'); // Import file 
fs.readFile('./a.txt',function(err,data){

// When the file data is read asynchronously, the code in the callback function is executed 
//err  Error object; 
//data  Data 
if(err){
 console.log(' Error reading file ');
return;
 }
 // Normal print data 
 console.log(data.toString());
})
console.log('111');

Write file writeFile

writeFileSync Read Data Synchronously

var fs = require ('fs'); //Introducing built-in modules

fs. writeFileSync ("b. txt", "I am the content written");

writeFile Asynchronous Data Read


fs.writeFile('c.txt',' I am writing the content ',function(err){
 if(err){
 console.log(' Error writing file ') ; 
 return;
}
console.log(' Error writing file ');

})
console.log('111');

Exercise


var fs=require('fs'); // Introducing file module 

fs.mkdirSync('./web1804'); // Create directories synchronously; 
fs.writeFileSync('./web1804/node1.txt',' Here are my study notes ') ; 
var data=fs.readFileSync('./web1804/node1.txt');
console.log(data.toString());

fs.mkdir('./web1804_1',function(err){
  if(err){
 console.log(' Failed to create directory ');
 return;
  }
  console.log(' Directory created successfully ');
})

var isExist=fs.existsSync('./web1804');

Expand

1. File deletion;

2. Create a multilevel directory d:/web1804/javascript/css

3. Delete the file directory

4. Copy files

Create directory fs. mkdir

Detect whether it is a file or a directory fs. stat

Write to append file fs. oppendFile

Read the directory fs. readdir

Rename rename

Delete directory rmdir

Delete file unlink

path


var path=require('path);
var psth1="http://www.baidu.com.cn/img.jpg " ; 

var index=psth1.indexOf('/');
console.log(index);

var lastindex=psth1.lastIndexOf('/'); // Gets the sequence number of the specified character in the string from back to front 
var sub=path1.substring(lastindex+1); //substring(index)  Returns the specified sequence number index Substring after 
console.log('sub:+'+sub);

path. basename Last Part of Return Path


var psth1="http://www.baidu.com.cn/img.jpg " ;  
var imgName=path.basename(path1); // Returns the end of the path 1 In part, I think this is used to get the file name or URL Parameters in the middle band 
console.log(imgName); 

path.jion

var paths=path.jion('web1804','html','css');

console.log(paths); //web1804\html\css

path. parse an object that returns a path string


var path1='http://www.baidu.com.cn/img.jpg';
var url=path.parse(path1);
console.log(url);

console. log (_ dirname); Current absolute path

Expand

substring (); Returns a substring after the specified sequence number index;

lastIdexOf gets the sequence number in the string in the specified character from back to front;

Supplementary knowledge: Application of url, path and http modules built into NodeJs

1. url module:


// Introduce url Module 
var url = require("url");

// Hypothesis 1 Web site 
var href = "http://www.baidu.com?name=jhh&age=20";

// Parse the URL into 1 A Url Object 
var obj = url.parse(href,true);
console.log(obj);

// Get Url Object in the query Object 
var query = obj.query;
console.log("queryName:"+query.name);
console.log("queryAge:"+query.age);

2. path module:


// Introduce path Module 
var p = require("path");

// Custom absolute path 
var path = "C:\\jhh\\text\\js";

// Remove the last 1 Layer 
console.log(p.dirname(path)); //C:\jhh\text

// Take the last 1 Layer 
console.log(p.basename(path)); //js

3. http module:


// Introduce http Module 
var http = require("http");

// Create web Server 
var server = http.createServer();

// Listening request 
server.on("request",function (request, response) {
  console.log(" Received user request: "+request.method);
  var url = request.url;
  var msg = "";
  if(url == "/"){
    msg = " This is the home page "
  }else if(url == "/login"){
    msg = " This is the login page "
  }else {
    msg = "404";
  }
  // Solve the corresponding garbled code 
  response.setHeader("content-type","text/html;charset=utf-8");

  // Corresponding data 
  response.write(msg);

  // End corresponding 
  response.end();
});

// Start the server 
server.listen(8081,function () {
  console.log(" Server startup ")
});

Related articles: