一 、自定义包上传npmjs
注册账号,注意: 记住要发送邮箱激活
切换你的npm源 nrm
列表:nrm ls
切换:nrm use npm
注意: 包的名称不能和别人的重复
步骤:
1.登录npm账号 : npm adduser
2. 上传: npm publish
二 、 node后端路由基本原理
它是后端框架 express 的路由的原理
后端路由其实就是一个web服务器
路由: ( 路: url路径)
很久以前: 多页面
index.html
< a herf = “./detail.html”>
< a herf = “./shopcar.html”>
现在流行: 单页面
index.html
这个时候,将detail/shopcar做成模板 [ ejs pug(jade) 【 Node.js专用】 art-template[ 后端使用 ] ],替换index.html里面的内容
const http = require(‘http’);
const host = ‘localhost’;
const port = 5000;
const fs = require(‘fs’);
http
.createServer((req, res) => {
res.writeHead(200, {
‘Content-type’: ‘text/html;charset=utf8’
})
switch (req.url) {
case ‘/home’:
res.write(‘home’)
res.end()
break;
case ‘/shopcar’:
fs.readFile(’./static/shopcar.html’, ‘utf8’, (error, docs) => {
res.write(docs)
res.end()
})
break;
case ‘/1.jpg’:
fs.readFile(’./static/1.jpg’, (error, docs) => {
// 图片是以二进制传输的
res.write(docs, ‘binary’)
res.end()
})
break;
case ‘/index.js’:
fs.readFile(’./static/js/index.js’, (error, docs) => {
// 图片是以二进制传输的
res.write(docs)
res.end()
})
break;
}
})
.listen(port, host, () => {
console.log(服务器运行在:http://${host}:${port}
)
})
三 、npm脚本 npm scripts
- 概念: npm脚本指的是package.json中的scripts字段
- 认识package.json
- package.json 是记录项目依赖包信息和npm脚本命令的一个配置文件
a. 项目依赖包信息:- dependencies 生产环境的依赖包
- devDependencies 开发环境使用的依赖包
b. 脚本命令:
- package.json 是记录项目依赖包信息和npm脚本命令的一个配置文件
- 在命令行执行的脚本命令
c. & vs &&- 举例:
- npm run dev & npm run app 全部执行完命令,最后输出结果
- npm run dev && npm run app 依次执行命令,分别跟着输出对应的结果
- 举例:
四 、 前端异步流程工具 【 王者/ 王牌 】
- 为什么要进行异步操作?
javascript是单线程,依次执行一个任务,要想让任务能够顺利进行,我们需要排队
异步就是将任务放入异步队列,在主线程执行结束之后再去执行 - 前端异步的操作方式
a. 传统方式- 回调函数
- 事件
b. 前端异步流程工具 【 封装出来函数、库 】 - es6 Promise
- es6 Generator函数
- es6 - es8 async 函数
- Node中的异步处理工具: nextTick setImmediate
- 第三方类库: async.js
- 参考资料
(1). Promise
https://blog.csdn.net/MrJavaweb/article/details/79475949
(2). Generator
https://www.cnblogs.com/imwtr/p/5913294.html
(3). Async-await- 里层请求数据结果返回到外层使用
- Async函数式generator函数 + spawn 自动执行器函数的 封装
( 4). Node.js 中的nextTick()和setimmediate()
https://www.cnblogs.com/5ishare/p/5268273.html
(5). async库
https://caolan.github.io/async/
参考文档
Event-loop
http://www.ruanyifeng.com/blog/2014/10/event-loop.html?bsh_bid=983729729
史上最易读懂的 Promise/A+ 完全实现
https://zhuanlan.zhihu.com/p/21834559