node学习笔记

news/2024/7/5 2:50:01

1.node.js的回调函数的两个参数:第一个参数代表错误信息,第二个参数代表结果。

if (err) {// 出错了
} else {// 正常
}
复制代码
注:当正常读取时,err参数为null,data参数为读取到的String。当读取发生错误时,err参数代表一个错误对象,data为undefined。

2.读取文件时,二进制文件和文本文件的相互转换。

// Buffer -> String
var text = data.toString('utf-8');
console.log(text);// String -> Buffer
var buf = Buffer.from(text, 'utf-8');
console.log(buf);
复制代码

3.读写文件的两种方式

// 第一种// 读
'use strict';
var fs = require('fs');
fs.readFile('sample.txt', 'utf-8', function (err, data) {if (err) {console.log(err);} else {console.log(data);}
});// 写
'use strict';
var fs = require('fs');
var data = 'Hello, Node.js';
fs.writeFile('output.txt', data, function (err) {if (err) {console.log(err);} else {console.log('ok.');}
});// 第二种// 读
'use strict';
var fs = require('fs');
// 打开一个流:
var rs = fs.createReadStream('sample.txt', 'utf-8');
rs.on('data', function (chunk) {console.log('DATA:')console.log(chunk);
});
rs.on('end', function () {console.log('END');
});
rs.on('error', function (err) {console.log('ERROR: ' + err);
});// 写
'use strict';
var fs = require('fs');
var ws1 = fs.createWriteStream('output1.txt', 'utf-8');
ws1.write('使用Stream写入文本数据...\n');
ws1.write('END.');
ws1.end();var ws2 = fs.createWriteStream('output2.txt');
ws2.write(new Buffer('使用Stream写入二进制数据...\n', 'utf-8'));
ws2.write(new Buffer('END.', 'utf-8'));
ws2.end();
复制代码
注:1.data事件表示流的数据已经可以读取了,end事件表示这个流已经到末尾了,没有数据可以读取了,error事件表示出错了。 2.所有可以读取数据的流都继承自stream.Readable,所有可以写入的流都继承自stream.Writable。 3.可以利用 pipe 方法把一个文件流和另一个文件流串起来.

http://lihuaxi.xjx100.cn/news/237144.html

相关文章

Web3与智能合约交互实战

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 Web3与智能合约交互实战 以太坊中智能合约和web3交互实战 最近区块链、以太坊十分的火,所有就会有许多人去进入区块链这个工作&#x…

git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改

git 覆盖本地修改When you learn to code, sooner or later youll also learn about Version Control Systems. And while there are many competing tools in this space, one of them is the de facto standard used by almost everyone in the industry. Its so popular tha…

你和区块链的距离就差这篇文章!

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 近年来,“区块链”逐渐成为热门话题,2018年各种关于区块链的行业资讯、投融资创业、技术和应用探索等集中爆发,…

JSP内置对象基础知识小结

JSP提供9大内置内象:一、request内象:封装了由客户端生成的HTTP请求的所有细节,主要包括了http头信息,系统信息,请求方式,请求参数等。1、获取访问请求参数:request.getParameter("arg&quo…

如何在5美元的Raspberry Pi上构建个人开发服务器

In this article, youll learn how to build a personal dev server by installing Git, Node.js, Rust, and Docker on a Raspberry Pi. The cheapest option costs just $5. You can get a starter kit ($25) for free here.在本文中,您将学习如何通过在Raspberry…

Extjs 基础篇—— Function基础

这里主要是JS的基础知识,也是深入理解Ext的基础。1.参数可变长,注意跟Java还是有一点区别的。例: view source print?1.function getUser(name,age){2.alert("name: "name " age: "age);3.}调用方法:getUse…

这个美国议员候选人想发币,联邦选举委员会还答应了

链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载。 佛罗里达州的一名国会候选人想给竞选志愿者发放基于以太坊的代币,以激励他们的工作,这是一项实验性的举措,而联邦…

如何有效使用每一点脑力总结_如何更有效地节省脑力和编码

如何有效使用每一点脑力总结如果您知道这些工具的存在,那么您现在可能会使用它们。 (If you knew these tools existed, youd probably be using them by now.) This article isn’t going to tell you about saving your neck with a Roost stand, or your wrists …