深入了解Node.js中的Koa框架


当前第2页 返回上一页

index.js 代码改造:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

// 加载模块

const fs = require('fs');

const koa = require('koa');

const mount = require('koa-mount');

 

const game = require('./game');

let playerWon = 0; // 赢的次数

 

const app = new koa();

// 精简内核,所有额外功能都移到中间件里实现。路由使用通过 mount 的中间件实现的

// 通过 mount() 把中间件挂载到一个特定的路径上,中间件独立于这个路径动作。

// /favicon.ico 路径的路由

app.use(

  mount('/favicon.ico', function (ctx) {

    // 对 `request` 和 `response` 的处理简化了,这两者都挂载在 `ctx` 上使用,返回的内容也可以通过直接赋值来使用

    ctx.status = 200;

    return;

  })

)

// mount中不可以跟多个函数中间件,可以通过 new koa() 来挂载在 koa 上:

const gameKoa = new koa();

app.use(

  mount('/game', gameKoa)

)

// 分离模块

gameKoa.use(

  async function (ctx, next) {

    if (playerWon >= 3) {

      // response.status(500);

      // response.send('我不会再玩了!');

      // 使用 = 赋值,更加简化了

      ctx.status = 500;

      ctx.body = '我不会再玩了!';

      return;

    }

    // 通过next执行后续中间件

    await next();

    // 当后续中间件执行完之后,会执行到这个位置

    if (ctx.playerWon) {

      playerWon++;

    }

  }

)

// 在 koa 里可以使用 async function 和 await next() 来执行异步中间件

// 使在异步的情况下也符合洋葱模型。

gameKoa.use(

  async function (ctx, next) {

    const query = ctx.query;

    const playerAction = query.action;

    if (!playerAction) {

      ctx.status = 400;

      return;

    }

    ctx.playerAction = playerAction;

    await next();

  }

)

// 异步处理,500ms后才返回结果

gameKoa.use(

  async function (ctx, next) {

    const playerAction = ctx.playerAction;

    const result = game(playerAction);

    // 对于一定需要在请求主流程里完成的操作,一定要使用await进行等待

    // 否则koa就会在当前事件循环就把http response返回出去了

    await new Promise(resolve => {

      setTimeout(() => {

        ctx.status = 200;

        if (result == 0) {

          ctx.body = '平局'

        } else if (result == -1) {

          ctx.body = '你输了'

        } else {

          ctx.body = '你赢了'

          ctx.playerWon = true;

        }

        resolve();

      }, 500)

    })

  }

)

// 打开页面 index.html

app.use(

  mount('/', function (ctx) {

    ctx.body = fs.readFileSync(__dirname + '/index.html', 'utf-8')

    return;

  })

)

// 监听端口 3000

app.listen(3000);

Express VS Koa

  • Express 门槛更低,Koa 更强大优雅。
  • Express 封装更多东西,开发更快速,Koa 可定制型更高。

它们孰“优”孰“劣”?

  • 其实框架之间并没有优劣之分
  • 不同的框架有不同的适用场景

更多编程相关知识,请访问:编程视频!!

以上就是深入了解Node.js中的Koa框架的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

node.js中的全局对象有哪些?有什么用?

如何在macos上安装node.js和npm

如何从node.js发送电子邮件

node.js和javascript区别是什么

4个使用将html转换为pdf的方法介绍

node.js搭建web服务器的方法

了解一下node.js casbin

node.js中require()是如何工作的?工作原理介绍

node.js+koa搭建一个简单后台管理系统

深入了解node.js中的koa框架

更多相关阅读请进入《node.js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...