第十一节:Express框架和Koa框架对比、nodemon的使用

一.  express和koa对比

1. 从架构的角度

  (1). express是完整和强大的,其中帮助我们内置了非常多好用的功能. (比如:路由、json解析等)

  (2). koa是简洁和自由的,它只包含最核心的功能,并不会对我们使用其他中间件进行任何的限制。

       koa中连最基本的get、post都没有给我们提供,需要通过自己或者路由来判断请求方式或者其他功能

2. 从执行顺序的角度

  (1).同步代码:express和koa在执行同步代码的时候没有区别。

  (2).异步代码:express在执行异步代码的时候,前面加await无效;koa执行异步代码的时候前面加await有效

       PS: 原因剖析详见下面

 

 

二. express同步和异步方法

1. 同步方法

   下面代码输出值:

         middleware01

         middleware02

         middleware03

   返回结果为:"ypf01ypf02ypf03"

分析:遇到next()的会调到下个中间件中执行

const express = require("express");
const app = express();
app.listen(9090, () => {
	console.log("------------express服务器启动成功了------------");
});

// 各种中间件
app.use((request, response, next) => {
	console.log("middleware01");
	request.msg = "ypf01";
	next();

	// 返回结果
	response.json(request.msg);
});

app.use((request, response, next) => {
	console.log("middleware02");
	request.msg += "ypf02";
	next();
});

app.use((request, response, next) => {
	console.log("middleware03");
	request.msg += "ypf03";
});

2. 异步方法

    express执行异步代码,无论前面加await 还是 不加await,都不会等待异步请求结果的,原理是因为这里的next返回的是void类型,不是promise,所以加await没有效果。

    下面的两个案例的结果相同,都是  

      运行结果:

           middleware01

           middleware02

           middleware03

      返回结果:ypf01ypf02哈哈哈哈

const express = require("express");
const axios = require("axios");
const app = express();
app.listen(9090, () => {
	console.log("------------express服务器启动成功了------------");
});

// 1. next前不加await
/* {
	app.use((request, response, next) => {
		console.log("middleware01");
		request.msg = "ypf01";
		next();

		// 返回结果
		response.json(request.msg);
	});

	app.use((request, response, next) => {
		console.log("middleware02");
		request.msg += "ypf02";
		next();
	});

	app.use(async (request, response, next) => {
		console.log("middleware03");
		// 网络请求
		const res = await axios.get("http://xxx:8000/home/xx");
		request.msg += res.data.title;
	});
}
 */

// 2. next前加await
{
	app.use(async (request, response, next) => {
		console.log("middleware01");
		request.msg = "ypf01";
		await next();

		// 返回结果
		response.json(request.msg);
	});

	app.use(async (request, response, next) => {
		console.log("middleware02");
		request.msg += "ypf02";
		await next();
	});

	app.use(async (request, response, next) => {
		console.log("middleware03");
		// 网络请求
		const res = await axios.get("http://xxx:8000/home/xxx");
		request.msg += res.data.title;
	});
}

 

三. koa同步和异步方法

1. 同步代码

   下面代码输出值:

         middleware01

         middleware02

         middleware03

    返回结果为:"ypf01ypf02ypf03"

   分析:遇到next()的会调到下个中间件中执行

const Koa = require("koa");
const app = new Koa();
app.listen(9090, () => {
	console.log("---------------koa服务器启动成功,端口9090----------------");
});
// 下面是各种中间件
app.use((ctx, next) => {
	console.log("middleware01");
	ctx.msg = "ypf01";
	next();
	// 返回结果
	ctx.body = ctx.msg;
});
app.use((ctx, next) => {
	console.log("middleware02");
	ctx.msg += "ypf02";
	next();
});
app.use((ctx, next) => {
	console.log("middleware03");
	ctx.msg += "ypf03";
});

 

2. 异步代码

(1).如果执行的下一个中间件是一个异步函数, 那么next默认不会等到中间件的结果, 就会执行下一步操作 【即 next()前面不加 await】

      运行结果:

           middleware01

           middleware02

           middleware03

      返回结果:ypf01ypf02

{
	app.use((ctx, next) => {
		console.log("middleware01");
		ctx.msg = "ypf01";
		next();
		// 返回结果
		ctx.body = ctx.msg;
	});
	app.use((ctx, next) => {
		console.log("middleware02");
		ctx.msg += "ypf02";
		next();
	});
	app.use(async (ctx, next) => {
		console.log("middleware03");
		// 网络请求
		const res = await axios.get("http://xxx:8000/home/xxx");
		ctx.msg += res.data.title;
	});
} 

(2). 如果我们希望等待下一个异步函数的执行结果, 那么需要在next函数前面加await  

     原理:这里的next的返回值是promise

      运行结果:

           middleware01

           middleware02

           middleware03

      返回结果:ypf01ypf02哈哈哈哈

{
	app.use(async (ctx, next) => {
		console.log("middleware01");
		ctx.msg = "ypf01";
		await next();
		// 返回结果
		ctx.body = ctx.msg;
	});
	app.use(async (ctx, next) => {
		console.log("middleware02");
		ctx.msg += "ypf02";
		await next();
	});
	app.use(async (ctx, next) => {
		console.log("middleware03");
		// 网络请求
		const res = await axios.get("http://xxxx:8000/home/xxx");
		ctx.msg += res.data.title;
	});
}

 

四. nodemon的使用

1. 背景

   之前运行 nodejs代码都是通过指令  【node  xxxx】,存在要给问题,每次修改代码都得关闭服务器,然后重新启动,使用nodemon启动后,每次修改代码服务器会自动重启。

2. 使用 

  (1). 全局安装nodemon  【npm install nodemon -g】

  (2). 使用nodemon启动相关代码即可。【nodemon  xxx】

 

 

 

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 
posted @ 2023-02-17 16:04  Yaopengfei  阅读(223)  评论(1编辑  收藏  举报