我是一个相对的初学者.我现在有两个单独的API调用(getPlayerAge和getPlayerRank),但我试图将这两个调用合并到一个端点(GetBothPlayerAgeAndRank)中,以便两个结果都在一个JSON中.我在ANGLE,TYPESCRIPT,Express,Node.js工作.我怎么做才能得到返回[getPlayerAge,getPlayerRank]?

import * as Promise from 'bluebird';
import { Request, Response, Router } from 'express';

import * as https from 'https';
import * as http from 'http';

import * as apicache from 'apicache';

import { PlayerModule } from '../modules';

const router = Router();
const cache = apicache.middleware;

router.get('/getPlayerAge',  (req: Request, res: Response, next: () => void) => {
    let playerName: string;
    if (!req.query.player) {
        res.json([]);
        next();
    } else {
        playerName = req.query.player;
        PlayerModule.getPlayerAge(playerName)
            .then((results) => {
                if (results.length > 0) {
                    res.json(results[0]);
                } else {
                    res.sendStatus(404);
                }
            })
            .catch((reason) => {
                //console.error(reason);
                res.sendStatus(500);
            })
            .finally(next);
    }
});


router.get('/getPlayerRank', (req: Request, res: Response, next: () => void) => {
    let playerName: string;
    if (!req.query.player) {
        res.json([]);
        next();
    } else {
        playerName = req.query.player;
        PlayerModule.getPlayerRank(playerName)
            .then((results) => {
                if (results.length > 0) {
                    res.json(results[0]);
                } else {
                    res.sendStatus(404);
                }
            })
            .catch((reason) => {
                //console.error(reason);
                res.sendStatus(500);
            })
            .finally(next);
    }
});

export const PlayerRoutes = router;

推荐答案

首先,创建一个中间件函数,您可以在其中判断此API的验证,就像播放器是否可用一样

//您也可以在单独的中间件文件中编写该中间件函数

API的基本原则是验证必须在控制器之前进行判断

checkValidation(req, res, next) {
    const { player } = req.query;
    if (!player) {
      res.status(404).json({message: "Player missing"});
      return;
    } else {
        next();
    }
}

然后

router.get('/getPlayerAge', checkValidation, async (req, res) => {
    const player = {req.query};
    const ageDetails = await PlayerModule.getPlayerAge(player);
    if(ageDetails && ageDetails.length > 0) {
        const rankDetails = await PlayerModule.getPlayerRank(player);
        res.status(200).json({message: "success", ageDetails, });
    } else {
        res.status(404).json({message: "No data found"});
    }
})

Node.js相关问答推荐

赫斯基添加命令已弃用?

DocuSign:调用createEntaine时,RequestJWTApplicationToken返回401 AUTHORIZATION_INVALID_TOKEN

MongoDB-$Lookup未获得适当的结果

try 使用Express和连接池将数据插入MySQL数据库时出现拒绝访问错误

类扩展值[object object]不是构造函数或null

在构建完成后,将AddedFiles文件夹的内容复制到dist文件夹

未授权使用联合身份未授权用户角色从 Amplify graphQL 访问类型 Y 上的 X

使用正则表达式查找文档,但输入是数组

使用 $in 查询时,如何获取 mongoDB 中每个唯一 ID 的 n 个文档?

解决并行保存到 mongodb

Node JS:自动 Select `http.get`与`https.get`

npm 不会安装 express 吗?

异步函数 - 等待不等待promise

Node_redis - 如何删除密钥?

如何在 AWS 上的 Amazon Linux AMI 中自动启动 node.js 应用程序?

错误:大多数中间件(如 bodyParser)不再与 Express Bundle

在 Node 中连接和缩小 JS 文件

Node.js 与 .net 中的异步/等待

我可以让 node --inspect 自动打开 Chrome

Mongoose - 验证邮箱语法