我的服务器中有以下路由:
//packages
const express = require('express');
const router = express.Router();
const rp = require('request-promise');
//date logic
var today = new Date();
var d = today.getDate();
var m = today.getMonth()+1; //January is 0!
var y = today.getFullYear();
if(d<10) { d = '0'+d }
if(m<10) { m = '0'+m }
today = y + m + d;
//needs error handling
//retrieve specific ocr records
router.get('/odds/:bid', (req,res,next) => {
//sports action API connection
const actionApi = {
url: `https://api-prod.sprtactn.co/web/v1/scoreboard/${req.params.bid}?date=${today}`,
json: true
}
//home team, away team, opening odds, and closing odds API pul
rp(actionApi)
.then((data) => {
const games = data.games
games.forEach((games) => {
games.teams.forEach((teams, i) => {
if (games.home_team_id == games.teams[i].id) {
homeTeam.push({home_team: games.teams[i].full_name});
} else if (games.away_team_id == games.teams[i].id) {
awayTeam.push({away_team: games.teams[i].full_name});
}
})
games.odds.forEach((odds, i) => {
if (games.odds[i].type == "game" && games.odds[i].book_id == "15") {
currOdds.push({
currAwayLine: games.odds[i].ml_away,
currHomeLine: games.odds[i].ml_home,
currAwaySpread: games.odds[i].spread_away,
currHomeSpread: games.odds[i].spread_home,
currAwayTotal: games.odds[i].total,
currHomeTotal: games.odds[i].total,
homeMlBets: games.odds[i].ml_home_public,
awayMlBets: games.odds[i].ml_away_public,
totalOverBets: games.odds[i].total_over_public,
totalUnderBets: games.odds[i].total_under_public,
spreadHomeBets: games.odds[i].spread_home_public,
spreadAwayBets: games.odds[i].spread_away_public
})
} else if (games.odds[i].type == "game" && games.odds[i].book_id == "30") {
openOdds.push({
openAwayLine: games.odds[i].ml_away,
openHomeLine: games.odds[i].ml_home,
openAwaySpread: games.odds[i].spread_away,
openHomeSpread: games.odds[i].spread_home,
openAwayTotal: games.odds[i].total,
openHomeTotal: games.odds[i].total
})
}
})
})
for (i = 0; i < homeTeam.length; i++) {
mergRecs.push({
homeTeam: homeTeam[i].home_team,
awayTeam: awayTeam[i].away_team,
currAwayLine: currOdds[i].currAwayLine,
currHomeLine: currOdds[i].currHomeLine,
openAwayLine: openOdds[i].openAwayLine,
openHomeLine: openOdds[i].openHomeLine,
currAwaySpread: currOdds[i].currAwaySpread,
currHomeSpread: currOdds[i].currHomeSpread,
openAwaySpread: openOdds[i].openAwaySpread,
openHomeSpread: openOdds[i].openHomeSpread,
currAwayTotal: currOdds[i].currAwayTotal,
currHomeTotal: currOdds[i].currHomeTotal,
openAwayTotal: openOdds[i].openAwayTotal,
openHomeTotal: openOdds[i].openAwayTotal,
homeMlBets: currOdds[i].homeMlBets,
awayMlBets: currOdds[i].awayMlBets,
totalOverBets: currOdds[i].totalOverBets,
totalUnderBets: currOdds[i].totalUnderBets,
spreadHomeBets: currOdds[i].spreadHomeBets,
spreadAwayBets: currOdds[i].spreadAwayBets
})
}
res.send(mergRecs)
})
.catch((err) => {
console.log(err);
});
})
module.exports = router; //make router exportable
带有 get 请求的 request-promise 调用外部 API。然后将来自外部 API 的请求解析为简化的有效负载。包装了 request-promise 的 get 请求然后返回这个减少的有效负载。第一次调用我的 get 请求时,它会正确返回有效负载,但是一旦您再次请求它,它会多次返回相同的有效负载。
我已经尝试在 get 请求中放置一个简单的响应,例如“res.send('hello world')”,并且 hello world 会返回正常的次数。但是由于某种原因,我的 request-promise 的有效负载在被调用时重复了获取请求。我似乎无法弄清楚为什么会这样。