One of my Serverless functions works perfectly fine in production but not in development

huangapple go评论65阅读模式
英文:

One of my Serverless functions works perfectly fine in production but not in development

问题

问题:我有6个无服务器函数,它们在生产环境中都运行得很好。我最近发现通过安装netlify-cli,我能够运行netlify dev,从而可以在开发应用程序时使用我的无服务器函数。

值得注意的是:
基本目录是客户端(client)。
构建命令是npm run build。
发布目录是客户端的build文件夹。

要运行netlify dev,我需要进入客户端文件夹,然后运行它。
现在所有内容都在我的客户端文件夹中。

这些是我的重定向规则,位于client/build中的一个名为_redirects的文件中。

/search  /.netlify/functions/search  200
/ranked  /.netlify/functions/ranked  200
/history  /.netlify/functions/history  200
/match  /.netlify/functions/match  200
/leaderboard  /.netlify/functions/leaderboard  200
/champion-mastery  /.netlify/functions/championMastery  200

我的排行榜功能在运行netlify dev时完全正常,但是,我的搜索功能会引发500错误,但我不知道为什么只有在运行netlify dev时才会出现这个问题,因为它在生产环境中可以正常工作。

这是在任何地方都能正常工作的排行榜功能:

const axios = require("axios");

async function handler(event, context) {
  try {
    const region = event.path.replace("/.netlify/functions/leaderboard/", "");
    const leaderboardResponse = await axios.get(
      `https://${region}.api.riotgames.com/lol/league-exp/v4/entries/RANKED_SOLO_5x5/CHALLENGER/I?page=1`,
      {
        headers: {
          "X-Riot-Token": process.env.RIOT_API_KEY,
        },
      }
    );

    // 返回排行榜数据
    return {
      statusCode: 200,
      body: JSON.stringify(leaderboardResponse.data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: "发生错误" }),
    };
  }
}

module.exports = { handler };

这是仅在生产环境中工作的搜索功能:

const axios = require("axios");

async function handler(event, context) {
  try {
    const region = event.path
      .replace("/.netlify/functions/search/", "")
      .split("/")[0];
    const summonerName = event.path
      .replace("/.netlify/functions/search/", "")
      .split("/")[1];

    // 向Riot Games API发出请求以获取玩家数据
    const summonerResponse = await axios.get(
      `https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${encodeURIComponent(
        summonerName
      )}`,
      {
        headers: {
          "X-Riot-Token": process.env.RIOT_API_KEY,
        },
      }
    );

    return {
      statusCode: 200,
      body: JSON.stringify(summonerResponse.data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: "发生错误" }),
    };
  }
}

module.exports = { handler };

我无法检查其他函数,因为其余的函数都在搜索后触发。

有任何想法为什么会出现这种情况吗?

英文:

Site Name: https://leaguetracker.netlify.app/

Issue: I have 6 serverless functions which all work perfectly fine in production. I have recently discovered that through installing netlify-cli I am able to run netlify dev which allows me to develop the app with use of my serverless functions..

It is worth noting the following
Base directory is client
Build command is npm run build
Publish directory is client/build

To run netlify dev I CD into the client folder then run it.
Everything now sits inside my client folder.

These are my redirects in a _redirects file which sit in client/build

/search  /.netlify/functions/search  200
/ranked  /.netlify/functions/ranked  200
/history  /.netlify/functions/history  200
/match  /.netlify/functions/match  200
/leaderboard  /.netlify/functions/leaderboard  200
/champion-mastery  /.netlify/functions/championMastery  200

My leaderboard function works perfectly fine when running netlify dev however, my search function throws a 500 error but I have no idea why this is only when running netlify dev as it works in production.

This is the leaderboard function that works everywhere

const axios = require("axios");

async function handler(event, context) {
  try {
    const region = event.path.replace("/.netlify/functions/leaderboard/", "");
    const leaderboardResponse = await axios.get(
      `https://${region}.api.riotgames.com/lol/league-exp/v4/entries/RANKED_SOLO_5x5/CHALLENGER/I?page=1`,
      {
        headers: {
          "X-Riot-Token": process.env.RIOT_API_KEY,
        },
      }
    );

    // Return the leaderboard data
    return {
      statusCode: 200,
      body: JSON.stringify(leaderboardResponse.data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: "An error occurred" }),
    };
  }
}

module.exports = { handler };


This is the search function that works only in production

const axios = require("axios");

async function handler(event, context) {
  try {
    const region = event.path
      .replace("/.netlify/functions/search/", "")
      .split("/")[0];
    const summonerName = event.path
      .replace("/.netlify/functions/search/", "")
      .split("/")[1];

    // Make a request to the Riot Games API to fetch player data
    const summonerResponse = await axios.get(
      `https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${encodeURIComponent(
        summonerName
      )}`,
      {
        headers: {
          "X-Riot-Token": process.env.RIOT_API_KEY,
        },
      }
    );

    return {
      statusCode: 200,
      body: JSON.stringify(summonerResponse.data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: "An error occurred" }),
    };
  }
}

module.exports = { handler };


I am unable to check any other functions as the rest of them are triggered after the search..

Any idea why this might be the case?

答案1

得分: 0

问题与如何传递 summonerName 到 URL 相关。URL 中的 summonerName 被双重编码,导致出现 %2520 而非 %20,这在详细错误中引发了 404 错误。

我已将搜索函数更新如下,已修复该问题。

const axios = require("axios");

async function handler(event, context) {
  try {
    const pathParts = event.path
      .replace("/.netlify/functions/search/", "")
      .split("/");
    const region = pathParts[0];
    const encodedSummonerName = pathParts[1];
    const summonerName = decodeURIComponent(encodedSummonerName);

    // 向 Riot Games API 发送请求以获取玩家数据
    const summonerResponse = await axios.get(
      `https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${encodeURIComponent(
        summonerName
      )}`,
      {
        headers: {
          "X-Riot-Token": process.env.RIOT_API_KEY,
        },
      }
    );

    return {
      statusCode: 200,
      body: JSON.stringify(summonerResponse.data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: "发生了错误" }),
    };
  }
}

module.exports = { handler };
英文:

The issue was related to how the summonerName is being passed in the URL. summonerName in the URL is double-encoded, resulting in %2520 instead of %20 which in the detailed error was throwing the 404 error.

I have updated my search function to the below which has fixed it.

const axios = require("axios");

async function handler(event, context) {
  try {
    const pathParts = event.path
      .replace("/.netlify/functions/search/", "")
      .split("/");
    const region = pathParts[0];
    const encodedSummonerName = pathParts[1];
    const summonerName = decodeURIComponent(encodedSummonerName);

    // Make a request to the Riot Games API to fetch player data
    const summonerResponse = await axios.get(
      `https://${region}.api.riotgames.com/lol/summoner/v4/summoners/by-name/${encodeURIComponent(
        summonerName
      )}`,
      {
        headers: {
          "X-Riot-Token": process.env.RIOT_API_KEY,
        },
      }
    );

    return {
      statusCode: 200,
      body: JSON.stringify(summonerResponse.data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: "An error occurred" }),
    };
  }
}

module.exports = { handler };

huangapple
  • 本文由 发表于 2023年7月11日 02:41:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定