美文网首页
http服务

http服务

作者: 林智_lz | 来源:发表于2022-02-17 20:05 被阅读0次

const fs = require('fs');

const game = require('./game')

const koa = require('koa');

const mount = require('koa-mount')

// 玩家胜利次数,如果超过3,则后续往该服务器的请求都返回500

var playerWinCount = 0

// 玩家的上一次游戏动作

var lastPlayerAction = null;

// 玩家连续出同一个动作的次数

var sameCount = 0;

const app = new koa();

app.use(

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

        // koa比express做了更极致的response处理函数

        // 因为koa使用异步函数作为中间件的实现方式

        // 所以koa可以在等待所有中间件执行完毕之后再统一处理返回值,因此可以用赋值运算符

        ctx.status = 200;

    })

)

const gameKoa = new koa();

app.use(

    mount('/game', gameKoa)

)

gameKoa.use(

    async function (ctx, next) {

        if (playerWinCount >= 3) {

            ctx.status = 500;

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

            return;

        }

        // 使用await 关键字等待后续中间件执行完成

        await next();

        // 就能获得一个准确的洋葱模型效果

        if (ctx.playerWon) {

            playerWinCount++;

        }

    }

)

gameKoa.use(

    async function (ctx, next) {

        const query = ctx.query;

        const playerAction = query.action;

        if (!playerAction) {

            ctx.status = 400;

            return;

        }

        if (sameCount == 9) {

            ctx.status = 500;

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

        }

        if (lastPlayerAction == playerAction) {

            sameCount++

            if (sameCount >= 3) {

                ctx.status = 400;

                ctx.body = '你作弊!我再也不玩了'

                sameCount = 9

                return;

            }

        } else {

            sameCount = 0;

        }

        lastPlayerAction = playerAction;

        ctx.playerAction = playerAction

        await next();

    }

)

gameKoa.use(

    async function (ctx, next) {

        const playerAction = ctx.playerAction;

        const result = game(playerAction);

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

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

        await new Promise(resolve => {

            // 模拟500毫秒后才返回的现象。

            setTimeout(() => {

                ctx.status = 200;

                if (result == 0) {

                    ctx.body = '平局'

                } else if (result == -1) {

                    ctx.body = '你输了'

                } else {

                    ctx.body = '你赢了'

                    ctx.playerWon = true;

                }

                resolve();

            }, 500)

        })

    }

)

app.use(

    mount('/', function (ctx) {

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

    })

)

app.listen(3000);

相关文章

  • $HTTP服务

    简介 Angular提供了http服务与后台做交互,用法简单,让我们看看Angular提供的GET、POS...

  • HTTP服务

    1.HTTP服务访问原理 浏览器看到页面的过程1.DNS解析过程2.建立三次握手过程 客户端 --web服务器建立...

  • http服务

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

  • angularJS $http服务

    //创建一个应用程序 var app=angular.module("myApp",[]); //创建控制器 ap...

  • $http服务应用

    $http服务的使用场景: then()函数:可以用来处理$http服务的回调,then()函数接受两个可选的函数...

  • HTTP 服务基础

    用户访问网站流程图 用户访问网站流程 1、用户浏览器输入网站www.happy.com回车,完成域名解析过程(DN...

  • 构建HTTP服务

    TCP与UDP都属于网络传输层协议,如果构造高效的网络应用,就应该从传输层进行着手。但是对于经典的应用层协议对于普...

  • Golang http服务

    开启http服务

  • iOS http服务

    1.下载CocoaHTTPServer,并加入project /*启动本地服务器端口/ 设置

  • Swoft HTTP 服务

    传统基于LNMP的Web架构中,Nginx作为Web服务器,PHP-FPM维护一个进程池去运行Web项目。简单、成...

网友评论

      本文标题:http服务

      本文链接:https://www.haomeiwen.com/subject/qvbslrtx.html