为了账号安全,请及时绑定邮箱和手机立即绑定

进击Node.js基础(二)

Scott 全栈工程师
难度中级
时长 2小时 4分
学习人数
综合评分9.60
153人评价 查看评价
9.8 内容实用
9.4 简洁易懂
9.6 逻辑清晰
  • // 用es6实现 const stream = require('stream') class ReadStream extends stream.Readable { constructor () { super() } _read () { this.push('i') this.push('love') this.push('imooc \n') this.push(null) } } class writStream extends stream.Writable { constructor () { super() } _write (chunk, encode, cb) { console.log(chunk.toString()) cb() } } class TransformStream extends stream.Transform { constructor () { super() } _transform (chunk, encode, cb) { this.push(chunk) cb() } _flush (cb) { this.push('oh yeah') cb() } } const rs = new ReadStream() const ws = new writStream() const ts = new TransformStream() rs.pipe(ts).pipe(ws)
    查看全部
  • Buffer用来保存原始数据(适合小文件,单个图片等,一次性全部存到buffer内存中),流是用来暂存和移动数据的(适合大文件,类似视频文件等,不用全部占用内存,通过流事件来实现边读边写的过程),两个常常结合使用比较好
    查看全部
  • 第一份代码,就是用传统的js来实现动画的时候,满足调教marginLeft==distance时,必须return停止迭代
    查看全部
  • "use strict"; const stream = require("stream"); class ReadStream extends stream.Readable{ constructor(){ super(); } _read(){ this.push("I "); this.push("Love "); this.push("You!\n"); this.push(null); } } class WriteStream extends stream.Writable{ constructor(){ super(); // this._cached = Buffer.from(""); } _write(chunk, encode, cb){ console.log(chunk.toString()); cb(); } } class TransformStream extends stream.Transform{ constructor(){ super(); } _transform(chunk, encode, cb){ this.push(chunk); cb(); } _flush(cb){ this.push("I Love You To!"); cb(); } } let readStream = new ReadStream(); let writeStream = new WriteStream(); let transformStream = new TransformStream(); readStream.pipe(transformStream).pipe(writeStream);
    查看全部
  • var fs = require('fs'); var readStream = fs.createReadStream('test.mp4'); var writeStream = fs.createWriteStream('write.mp4'); readStream.on('data',function(chuck){ if(writeSream.write(chunk)===false){//判断是否将数据写出缓存区,如果是false暂停读取 readStream.puse(); } }); readStream.on('end',function(){ console.log('读取完成'); writeStream.end(); }); writeStream.on('drain',function(){ readStream.resume();//开始读 });
    查看全部
  • 谷百一下搬运笔记: 2015年发布了ES6标准,所谓 Promise,就是ES6标准的一个对象,用来传递异步操作的消息。它代表了某个未来才会知道结果的事件(通常是一个异步操作),并且这个事件提供统一的 API,可供进一步处理。 有了 Promise 对象,就可以将异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函数。此外,Promise 对象提供统一的接口,使得控制异步操作更加容易。 var promise = new Promise(function(resolve, reject) { if (/* 异步操作成功 */){ resolve(value); } else { reject(error); } }); promise.then(function(value) { // success }, function(value) { // failure }); Promise函数接受一个函数作为参数,该函数的两个参数分别是 resolve 方法和 reject 方法。 如果异步操作成功,则用 resolve 方法将 Promise 对象的状态,从「未完成」变为「成功」(即从 pending 变为 resolved); 如果异步操作失败,则用 reject 方法将 Promise 对象的状态,从「未完成」变为「失败」(即从 pending 变为 rejected)。
    查看全部
  • 用流方法创建response
    查看全部
  • Promise 库
    查看全部
  • api nodejs
    查看全部
    1 采集 收起 来源:Node.js课程前言

    2016-04-04

  • 安装emmet插件,实现tab键自动填充。

    按下Ctrl+D ----------- 可以继续向下同时选中下一个相同的文本进行同时编辑。

    查看全部
  • function promiseAnimate(ball,distance){ console.log(ball) return new Promise(function(resolve,reject){ function _animate(){ setTimeout(function(){ console.log("animate") var marginLeft = parseInt(ball.style.marginLeft,10) if(marginLeft == distance){ return resolve() }else{ if(marginLeft < distance)marginLeft ++ else{marginLeft --} } ball.style.marginLeft = marginLeft + "px" _animate() },13 ) } _animate() }) }
    查看全部
  • ball.style.marginLeft,这样取属性必须写在html中, ball.style.marginLeft = marginLeft +"px"
    查看全部
  • <script> var ball1 = document.querySelector(".ball1"); var ball2 = document.querySelector(".ball2"); var ball3 = document.querySelector(".ball3"); function animate(ball, distance, callBack) { setTimeout(function() { var marginLeft = parseInt(window.getComputedStyle(ball, null).marginLeft, 10); if (marginLeft == distance) { callBack(); } else { if (marginLeft < distance) { marginLeft++; } else { marginLeft--; } ball.style.marginLeft = marginLeft + "px"; animate(ball, distance, callBack); } }, 13) } animate(ball1, 100, function() { animate(ball2, 200, function() { animate(ball3, 300, function() { animate(ball3, 150, function() { animate(ball2, 150, function() { animate(ball1, 150) }) }) }) }) }) </script>
    查看全部
  • 可读流负责读取外部数据,并把数据缓存到内部buffer数组;可写流负责消费数据,从内部buffer数据获取数据,然后对得到的chunk块进行处理。
    查看全部
  • pipe方法会自动监听data和end时间,还可以自动控制后端压力,在客户端链接缓慢时,自动控制流量,并且只有在pipe末端的目标流真正需要数据的时候才会从源头取得数据
    查看全部

举报

0/150
提交
取消
课程须知
本课程是一个系列课程,前导课程是《进击 Node.js 基础(一)》,所以建议小伙伴们学习本课程之前先把它拿下。
老师告诉你能学到什么?
1、了解 Promise 2、横扫 Nodejs API:Buffer、API-Stream

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!