node.js的参数
request : 表示请求
response : 表示响应,给前端发送的信息
后端路由基本原理
- 后端路由其实就是一个web服务器
- 引入http
举例
const http = require( 'http' )
const host = 'localhost'
const fs = require( 'fs' )
const port = 5000
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;
default:
break;
}
})
.listen( port,host,() => {
console.log( `服务器运行在:http://${ host }:${ port }` )
})
后端框架 express
路由:实现单页面的转换
图片是以二进制传输的
通过http
npmscript
- npm脚本:指的是package.json中的scripts字段
在script字段中添加 任务
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node index.js",
"app": "node app.js"
},
其中:
npm run a.js & npm run b.js
npm run a.js && npm run b.js
一个&两个执行完在输出
两个&& 执行一个输出一个 依次执行
前端异步流程
1.为什么要进行异步操作?
- javascript是单线程,依次执行一个任务,想要让任务能够顺利进行
- 异步就是将任务放在异步队列中,在主线程执行结束后再去执行
2.前端异步流程的操作方式
- 传统方式
- 回调函数
- 事件
前端异步流程工具 - es6 Promise
- es6 Generator
- es6-es8 async 函数
- Node 中的异步处理工具:nexTick()setImmediate()
- 第三方库:async.js
执行顺序
主线程 nextTick 回调函数 setImmidiate
参考资料
- Promise
https://blog.csdn.net/MrJavaweb/article/details/79475949 - Generator
https://www.cnblogs.com/imwtr/p/5913294.html - Async-await
- 里层请求数据结果返回到外层使用
- Async函数式generator函数 + spawn 自动执行器函数的 封装
- Node.js 中的nextTick()和setimmediate()
https://www.cnblogs.com/5ishare/p/5268273.html - 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