使用 es 6/7 特性开发 node 项目
TRANSCRIPT
![Page 1: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/1.jpg)
使⽤用 ES6/7 特性 开发 Node 项⺫⽬目
2015/11/21李成银
![Page 2: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/2.jpg)
关于我• 李成银@奇舞团 • 7 年⼯工作经验 • ThinkJS 作者 • 擅⻓长⼯工程化解决⽅方案 • https://github.com/welefen
![Page 3: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/3.jpg)
⼤大纲• 异步回调问题 • ⾃自动更新问题 • ThinkJS 介绍
![Page 4: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/4.jpg)
缩写• ES6:ECMAScript 6,ECMAScript 2015 • ES7:ECMAScript 7 • 编译:转译 • Node: Node.js node.js
![Page 5: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/5.jpg)
Node 特点• ⾼高并发 • 事件驱动 • IO 密集型
![Page 6: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/6.jpg)
异步问题
![Page 7: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/7.jpg)
异步处理⽅方式• callback • Promise • ES6 generators • ES7 async functions
![Page 8: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/8.jpg)
Callback
![Page 9: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/9.jpg)
callback
![Page 10: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/10.jpg)
callback
![Page 11: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/11.jpg)
Promise
![Page 12: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/12.jpg)
Promise
![Page 13: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/13.jpg)
Promise
![Page 14: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/14.jpg)
Callback => Promise
![Page 15: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/15.jpg)
Promise 的问题• this 指向问题没有解决 • 局部变量需要借助外层的临时变量才能跳跃向后传递 • ⽆无法直接跳过中间某些环节 • 隐藏错误
![Page 16: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/16.jpg)
Generators
![Page 17: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/17.jpg)
Generators
• 使⽤用 function * 和 yield • 是迭代器的⼀一个⼦子类型
![Page 18: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/18.jpg)
Generators
![Page 19: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/19.jpg)
Generators 的问题• 语义上不易理解 • 需要借助执⾏行器 • ⽆无法和 Arrows ⼀一起使⽤用 • yield 和 yield * • function * 和 * method
![Page 20: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/20.jpg)
Async functions
![Page 21: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/21.jpg)
Async functions
![Page 22: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/22.jpg)
Async functions
• 基于 Promise • 返回 Promise • 使⽤用 async/await 更加语义性 • ⽀支持和 Arrows ⼀一起使⽤用 • 不⽤用借助第三⽅方模块
![Page 23: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/23.jpg)
Async functions 错误捕获
try/catch 捕获错误
![Page 24: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/24.jpg)
Async functions 错误捕获
特定值判断是否有错误
![Page 25: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/25.jpg)
Async functions 并⾏行处理
![Page 26: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/26.jpg)
Async functions 问题
• Stage: Proposal • ⺫⽬目前还没有⽀支持的 Runtime
![Page 27: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/27.jpg)
![Page 28: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/28.jpg)
https://github.com/facebook/regenerator
![Page 29: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/29.jpg)
编译带来的问题• ⽂文件修改后如何⾃自动编译
• 如何精准定位报错信息--watch
--retain-lines
• 如何断点调试只能断点调试编译后的代码
![Page 30: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/30.jpg)
使⽤用更多的特性• class • Arrows • Enhanced Object Literals • Template Strings • Default + Rest + Spread • …
⽆无需担⼼心当前的 Node 环境是否⽀支持
![Page 31: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/31.jpg)
性能如何?
![Page 32: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/32.jpg)
class
![Page 33: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/33.jpg)
generators
![Page 34: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/34.jpg)
⾃自动更新问题
![Page 35: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/35.jpg)
fs.watch?chokidarnodemon
![Page 36: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/36.jpg)
⽆无法保存临时数据
![Page 37: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/37.jpg)
更好的解决⽅方案• 监听⽂文件修改变化 • 热更新修改的⽂文件 • 以及依赖⽂文件
![Page 38: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/38.jpg)
require.cache
![Page 39: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/39.jpg)
cache 格式
![Page 40: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/40.jpg)
重写 require ⽅方法
lib/module.js
![Page 41: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/41.jpg)
清除缓存
![Page 42: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/42.jpg)
2014/09/22 发布 1.0 版本2015/10/30 发布 2.0 版本
![Page 44: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/44.jpg)
Async functions
![Page 45: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/45.jpg)
Generators
![Page 46: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/46.jpg)
Promise
![Page 47: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/47.jpg)
特性• ⾃自动编译、⾃自动更新 • 3 种项⺫⽬目模式:mini、normal、module • 3 种项⺫⽬目环境:development、testing、production • 3 种运⾏行环境:ES5、ES6/7->ES5、ES6
![Page 48: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/48.jpg)
更多特性•⽀支持 Mysql,MongoDB,Redis 数据库 • ⽀支持 socket.io,SockJS 等 WebSocket库 • ⽀支持命令⾏行执⾏行和定时任务 • ⽀支持国际化和多主题 • ⽀支持 Middleware 和 Adapter • ⽀支持 __before,__after,__call • ⽀支持⾃自动创建 REST API • …
![Page 49: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/49.jpg)
稳定性和性能• 1500+ 测试⽤用例,95% • 完善的⽂文档,中⽂文/英⽂文,PDF • 2000+ commits
![Page 50: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/50.jpg)
• https://www.thinkjs.org • https://github.com/75team/thinkjs • QQ 交流群:339337680
![Page 51: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/51.jpg)
¥5000/年奖励社区贡献者
![Page 52: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/52.jpg)
4 + 2
![Page 53: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/53.jpg)
ThinkJS 微信红包群
![Page 54: 使用 ES 6/7 特性开发 Node 项目](https://reader034.vdocuments.pub/reader034/viewer/2022042423/58f2f5491a28abc3128b4591/html5/thumbnails/54.jpg)
谢谢⼤大家