• 289 node.js系统模块,相对路径、绝对路径




    05.readFile.js

    // 1.通过模块的名字fs对模块进行引用
    const fs = require('fs');
    
    // 2.通过模块内部的readFile读取文件内容
    fs.readFile('./01.helloworld.js', 'utf8', (err, doc) => {
        // 如果文件读取出错err 是一个对象 包含错误信息
        // 如果文件读取正确 err是 null
        // doc 是文件读取的结果,也就是文件读取的内容
        console.log(err);
        console.log(doc);
    });
    

    const fs = require('fs');
    
    fs.writeFile('./demo.txt', '即将要写入的内容', err => {
      if (err != null) {
        console.log(err);
        return;
      }
    
      console.log('文件内容写入成功');
    })
    

    // public/uploads/avatar
    const path = require('path');
    
    const finalPath = path.join('public', 'uploads','avatar');
    
    console.log(finalPath); // publicuploadsavatar
    

    const fs = require('fs');
    const path = require('path');
    
    // 当前文件的绝对路径,...kejian211-16.前后端交互11-13.node+expressday01code
    console.log(__dirname);
    // ...kejian211-16.前后端交互11-13.node+expressday01code1.helloworld.js
    console.log(path.join(__dirname, '01.helloworld.js'));
    
    fs.readFile(path.join(__dirname, '01.helloworld.js'), 'utf8', (err, doc) => {
      console.log(err)  // null
      console.log(doc)  // 01.helloworld.js 的内容
    });
    
    
  • 相关阅读:
    inotify事件监控
    NFS网络文件共享服务
    Rsync数据同步服务
    SSH连接原理及ssh-key讲解
    C语言I博客作业04
    C语言l博客作业03
    C语言I博客作业02
    定义一个计算字符串高度的方法
    字典转模型
    UIScrollView和UIPageControl
  • 原文地址:https://www.cnblogs.com/jianjie/p/12329982.html
Copyright © 2020-2023  润新知