后端路由其实就是一个web服务器
http://localhost:5000/shopcar
路由:
路: 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 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 }` )
})
- async
async function p1() {
const result = await '任务一'
return result
}
const p2 = async function () {
const result = await '任务二'
return result
}
const p3 = async () => {
const result = await '任务三'
return result
}
console.log(p1().then(res => console.log(res)))
console.log(p2().then(res => console.log(res)))
console.log(p3().then(res => console.log(res)))
console.log(' 主线程')
- 多任务执行
const { series,parallel } = require('async')
series 表示 串行
series([任务一,任务二],callback)
串行: 表示两个任务在一个任务线上,任意一个任务崩溃,其余任务全部无法执行
series([
function ( callback ) {
callback( null, '任务一')
},
function ( callback ) {
setTimeout(() => {
callback( null, '任务二')
},2000)
}
],function (error,result ) {
console.log('任务六')
console.log( result )
})
// parallel 并行
parallel([
function ( callback ) {
callback( null, '任务三')
},
function ( callback ) {
setTimeout(() => {
callback( null, '任务四')
},2000)
}
],function (error,result ) {
console.log('任务五')
console.log( result )
})
console.log('主线程')
-
generator生成器函数
- function 关键字后面跟一个*来定义一个generator函数
- 使用yield关键字来定义任务
function* p1 () {
// yield 任务
yield '任务一'
yield '任务二'
yield '任务三'
yield '任务四'
yield '任务五'
return '任务六'
}
const task = p1() // 迭代器Iterator对象
console.log( task.next() ) // 通过next方法来遍历执行任务
console.log( task.next() ) // 通过next方法来遍历执行任务
console.log( task.next() ) // 通过next方法来遍历执行任务
console.log( task.next() ) // 通过next方法来遍历执行任务
console.log( task.next() ) // 通过next方法来遍历执行任务
console.log( task.next() ) // 通过next方法来遍历执行任务
- Node.js中的异步流程工具
- Node.js全局变量: global
process.nextTick( () => {
console.log('A')
})
setImmediate(() => {
console.log('C')
})
process.nextTick( () => {
console.log('B')
})
console.log( '主线程任务' )
- promise.js
const p1 = new Promise(( resolve,reject ) => {
resolve( '任务一' )
}).then( data => {
console.log( data )
})
.catch( error => {
if ( error ) throw error // 抛出一个错误,这个时候进程会被终端
})
const p2 = new Promise(( resolve,reject ) => {
setTimeout(() => {
resolve( '任务二' )
},2000)
}).then( data => {
console.log( data )
})
.catch( error => {
if ( error ) throw error // 抛出一个错误,这个时候进程会被终端
})
/* // Promise.all([promise实例1,promise实例2])
// all中,一次执行任务,即使有延时任务,也必须等待延时任务结束,后续任务才能进行
Promise
.all([p1,p2])
.then( () => {
console.log( '任务三' )
})
*/
Promise
.race([p1,p2]) // race指的是 任务快 的先进行,其他延时任务后续进行
.then(() => {
console.log('任务三')
})
console.log('主线程')