node开发帮助文档

  1. Buffer

每个元素大小为1字节,类似于数组

  1. Buffer.alloc()

     创建一个Buffer  参数1:字节数

     Const buf_1 = Buffer.alloc(10)

  1. Buffer.allocUnsafe()

     创建一个Buffer  参数1:字节数

     Const buf_1 = Buffer.allocUnsafe(10)

  1. Buffer.from()

    字符串转Buffer

     const buf_3 = Buffer.from('iloveyou');

  1. Buffer.toString()

    buffer转字符串

     const str = buf_3.toString()

  1. buffer读取和写入

console.log(buf_3[0]);  //读取  

 buf_3[0] = 99;         //设置

  1. 关于溢出

溢出的高位数据会舍弃

//关于溢出   舍弃高于 8 位的内容  0001 0010 1100‬  =>  0010 1100‬  => ‬‬‬‬‬‬‬‬‬‬‬‬

// buf_3[0] = 300;

// console.log(buf_3.toString());

  1. 关于中文

一个 UTF-8 的中文字符大多数情况都是占 3 个字节

//关于中文 一个UTF-8中文字符占据三个字节

// const buf_4 = Buffer.from('我爱你');

// console.log(buf_4);

  1. 文件系统fs

fs 全称为 file system,是 NodeJS 中的内置模块,可以对计算机中的文件进行增删改查等操作。

  1. 简单的写入

 * fs.writeFile(file, data, [,options], callback);

  * fs.writeFileSync(file, data);

  * options 选项

    * `encoding` **默认值:** `'utf8'`

    * `mode`**默认值:** `0o666`

* `flag` **默认值:** `'w'`

## flag标记

* r   read  只读

* w   write 可写

* a   append  追加

## 0o666  Linux 下文件权限的管理方式

* 6   所有者的权限  index.html

* 6   所属组的权限  

* 6   其他人权限

6 怎么来的 4+2

* 4   可读

* 2   可写

* 1   可执行

chmod 0777 -R www

chmod 0755 文件

chmod 0644 文件夹  

chmod 000 -R  /

/文件写入

//1. 引入 fs 模块

const fs = require('fs');

 异步API  调用 fs 中的方法 write 写入  File 文件

fs.writeFile('文件路径', '写入内容', { flag: 'a' }, function (err) {

    if (err) {

        console.log('失败的错误为' + err);

        return;

    }

    console.log('写入成功');

});

 同步API

// fs.writeFileSync('./app.css', '*{margin:0;padding:0}');

// fs.writeFileSync('./app.js', Date.now());

// console.log(Date.now());

如果要做服务, 需要使用『异步』

* 如果做文件相关的处理,  不涉及为用户提供服务, 可以使用同步API(简单写入, 读取)

  1. 流式写入

 * fs.createWriteStream(path,[options])

    * path

    * options

      * ==flags==   **默认值:** `'w'`

      * `encoding `**默认值:** `'utf8'`

      * `mode`   **默认值:** `0o666`

    * 事件监听 open  close  eg:  ws.on('open', function(){});

//1. 引入 fs 模块

const fs = require('fs');

//2. 创建写入流对象

const ws = fs.createWriteStream('./home.html');

//3. 执行写入

// ws.write('<html>');

// ws.write(`

//     <head>

//         <title>这是一个脚本创建的文件哦</title>

//     </head>

//     <body>

//         <h1>哎呦 不错哦~</h1>

//     </body>

// `)

// ws.write('</html>');

ws.write(`

    const body = document.body;

    body.style.background = 'pink';

    setTimeout(() => {

        alert('恭喜中奖啦!!!');

    }, 1000);

`)

//4. 关闭写入流

ws.close();

## writeFile 与 createWriteStream 使用场景

对于简单的写入次数较少的情况, 可以使用 writeFile , 如果是批量要写入的场景,使用 createWriteStream

  1. 简单读取

* fs.readFile(file, function(err, data){})

* fs.readFileSync(file)

//1. 引入 fs 模块

const fs = require('fs');

//2-1. 调用方法读取内容

// fs.readFile('./home.html', (err, data) => {

//     if(err){

//         console.log('读取失败');

//         console.log(err);

//         return;

//     }

//     //输出从文件中读取的内容

//     console.log(data.toString());

// });

//2-2 同步读取

let result = fs.readFileSync('./index.html');

console.log(result.toString());

  1. 流式读取

* fs.createReadStream();

//1. 引入 fs 模块

const fs = require('fs');

//2. 创建读取流对象

const rs = fs.createReadStream('./file/刻意练习.mp3');

//3. 绑定事件 when 当....时候   chunk 块   当读取完一块数据后 触发

rs.on('data', (chunk) => {

    console.log(chunk.length);

}); 

// 读取流打开的时候触发

rs.on('open', () => {

    console.log('读取流打开了');

});

  1. 读取并写入

//复制文件

//1. 模块引入

const fs = require('fs');

//2. 创建流对象

const ws = fs.createWriteStream('./file/不二法门.mp3');

const rs = fs.createReadStream('./file/刻意练习.mp3');

//3. 绑定事件读取内容

// rs.on('data', chunk => {

//     //写入文件

//     ws.write(chunk);

// });

//pipe管道

rs.pipe(ws);

## 读取文件常见错误

[Error: ENOENT: no such file or directory, open 

没有找到目标文件, 遇到此情况需要仔细检查目标路径

## 操作系统的路径分隔符

* windows   \

* Linux     /

## readFile 与 createReadStream

* 对于小文件读取和处理 readFile

* 对于大文件读取      createReadStream

  1. 文件删除

* fs.unlink('./test.log', function(err){});

* fs.unlinkSync('./move.txt');

//1. 引入 fs 模块

const fs = require('fs');

//2. 调用方法

// fs.unlink('./project/index.js', err => {

//     if(err) throw err;

//     console.log('删除成功');//..

// });

// fs.unlinkSync('./project/app.js');

  1. 移动文件+重命名

* fs.rename('./1.log', '2.log', function(err){})

* fs.renameSync('1.log','2.log')

// 1. 引入模块

const fs = require('fs');

//2. 调用方法

// fs.rename('./home.js', './index.js', err => {

//     if(err) throw err;

//     console.log('重命名成功');

// });

// fs.rename('./index.html', './project/首页.html', err => {

//     if(err) throw err;

//     console.log('重命名成功');

// });

//同步API

fs.renameSync('./project/app.css', './project/index.css');

  1. 文件夹操作

* fs.rename('./1.log', '2.log', function(err){})

* fs.renameSync('1.log','2.log')

// 1. 引入模块

const fs = require('fs');

//2. 调用方法

// fs.rename('./home.js', './index.js', err => {

//     if(err) throw err;

//     console.log('重命名成功');

// });

// fs.rename('./index.html', './project/首页.html', err => {

//     if(err) throw err;

//     console.log('重命名成功');

// });

//同步API

fs.renameSync('./project/app.css', './project/index.css');

9路径问题

## fs 模块中的相对路径 参照的是执行命令时的工作目录

// fs 模块的路径问题

/**

 * 路径的分类

 * 绝对路径

 *   * D:/www/share/day05/代码/1-nodejs/write.js (windows)

 *   * C:/images/logo.png  (windows)

 *   * /usr/root/www/website/index.html (linux)

 * 相对路径

 *   * ./index.html

 *   * ../css/app.css  上一级目录中找 css/app.css

 *   * index.html  当前文件夹下的 index.html

 */ 

const fs = require('fs');

// fs.writeFileSync('D:\\www\\share\\day05\\课堂\\1-NodeJS\\代码\\3-文件系统\\index.html','abc');

//特殊的变量  『始终保存的是当前文件所在文件夹的绝对路径』

// console.log(__dirname);

fs.writeFileSync(__dirname + '/index.html','abc');

10Stat

// stat 查看『资源的状态』

const fs = require('fs');

//查看文件状态

fs.stat('./file', (err, stats) => {

    if(err) throw err;

    //如果没有错

    console.log('是否为文件夹' + stats.isDirectory());

    console.log('是否为文件' + stats.isFile());

});

(11)unicode 字符集

* https://www.tamasoft.co.jp/en/general-info/unicode.html

* https://www.cnblogs.com/whiteyun/archive/2010/07/06/1772218.html    

暴露  module.exports={}

导入  const me = require(‘路径’)

  • 38
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值