• node-fs文件系统模块


    在node环境中所有与文件操作都是通过fs核心模块实现的。包括文件的创建、删除、查询以及读写和写入

    在 fs 模块中,所有的方法都分为同步和异步两种实现,具有 sync 后缀的方法为同步方法,不具有 sync 后缀的方法为异步方法

    文件读取

    同步文件读取readFileSync

    第一个参数为读取文件的路径或文件描述符;

    第二个参数为 options,默认值为 null

    const fs = require('fs');
    fs.readFile('./www/1.txt',(error,data)=>{
       if(error){
        console.log('404');
      }else{
        console.log(data.toString());
      }
    });
    //readFileSycn只有成功状态,找不到就报错
    try {
        let data = fs.readFileSync('./www/1.txt');
        console.log(data.toString());
    } catch (error) {
        
    }
    console.log(11111);

    异步文件读取readFile

    第一个参数为读取文件的路径或文件描述符

    第二个参数为 options,默认值为 null

    第三个参数为callback回调函数

    函数内有两个参数 err(错误)和 data(数据),该方法没有返回值,回调函数在读取文件成功后执行

    //文件的读取
    fs.readFile("1.txt",function (err,data) {
         if (err){
            console.log(err)
         }else {
             console.log(data.toString())
         }
     })

    文件写入

    同步文件写入writeFileSync

    第一个参数为写入文件的路径或文件描述符

    第二个参数为写入的数据,类型为 String 或 Buffer

    第三个参数为 options,默认值为 null

    const fs = require("fs");
    fs.writeFileSync("2.txt", "Hello world");
    let data = fs.readFileSync("2.txt", "utf8");
    console.log(data);

    异步文件写入writeFile

    第一个参数为读取文件的路径或文件描述符

    第二个参数为 options,默认值为 null

    let fs = require('fs');
    
    fs.writeFile('./www/2.txt','dsnadkjsjdksa',(error)=>{
        if(error){
             console.log('失败');
        }else{
             console.log('成功');
        }
     })

    文件删除

    同步文件删除unlinkSync

    const fs = require("fs");
    fs.unlinkSync("a/inde.js");

    异步文件删除unlink

    const fs = require("fs");
    fs.unlink("a/index.js", err => {
        if (!err) console.log("删除成功");
    });

    栗子—判断URL是接口还是请求静态资源(页面)

    思路:
    如果url里面包含?就说明请求的是接口
    否则是静态文件。需要拼地址

    let http = require('http');//引入http模块用来构建服务器
    let fs = require('fs');//需要读写删等操作时候就引入fs文件系统模块
    http.createServer((req,res)=>{
        //判断是请求接口还是请求静态页面
        let url = req.url;
        if(url.includes('?')){//如果url包含?代表请求的是接口
            //向客户端发送头信息(防止中文乱码)
            res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
            res.write('每天进步一点点');
            res.end();
        }else{//否则请求的是静态页面
        // 斜杠代表 localhost
         //默认出现index文件
            if(url === '/') {url = '/inex.html';}
                //读入文件
                fs.readFile('./www'+url,(err,data)=>{
                  if(err){
                      //如果没有文件,就读取404页面
                      let data = fs.readFileSync('./www./404.html');
                      res.write(data);
                  }else{
                      //如果有文件就把文件发给前台
                      res.write(data);
                  }
                  res.end();//写入结束
                });
        }
    }).listen(80);//监听端口
  • 相关阅读:
    转 Python学习(九)
    转 Python学习(八)
    转 Python学习(七)
    转 Python学习(六)
    转 Python学习(五)
    转 Python学习(四)
    转Python学习(三)
    转Python学习(一)
    面向对象第三章(向上造型、重写、重载)
    面向对象第一章(成员变量、局部变量、重载)
  • 原文地址:https://www.cnblogs.com/theblogs/p/10651426.html
Copyright © 2020-2023  润新知