You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 12部署AWS Amplify后API路由返回500错误求助

Next.js 12部署AWS Amplify后API路由返回500错误(XML解析错误)

问题详情

使用Next.js 12开发项目,首页调用/api/users/limit-exceed接口。该接口在本地开发环境和自建生产环境均能正常运行,但部署到AWS Amplify后,组件通过Axios发送请求时,API路由返回500错误,报错信息为XML Parsing Error。

除/api/*相关路由外,客户端其他功能均正常。在GitHub上发现有用户遇到类似问题,但未找到明确解决方案。

备注:使用dotenv-vault管理环境变量,客户端环境变量正常,但服务端环境变量无法正常工作。

出错的API路由代码

import supabase from "pages/api/_supabase";
    
export default async function handler(req, res) {
    if (req.method === "GET") {
        console.log("body:", req.body)
        try {
            const data = await supabase.from("users").select('*', { count: 'exact', head: true })
            
            if (process.env.USER_SIGNUP_LIMIT <= data.count) {
                res.status(200).json({
                    data: true,
                    users: data.count
                });
            } else {
                res.status(200).json({
                    data: false,
                    users: data.count
                });
            }
        } catch (error) {
            console.log("message:", error.message)
            res.status(404).json({ data: "An ERROR OCCUR :" + JSON.stringify(error) });
        } 
    } else {
        // Handle other HTTP methods
        res.status(405).json({ data: "Method not allowed" });
    }
}

AWS Amplify构建配置文件

# AWS Amplify build settings.

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm install
        
    build:
      commands:
        - env | grep  -e DOTENV_KEY  >> .env.production
        - npm run build
        - cp -r .next/static .next/standalone/.next/static
        - cp -r public .next/standalone/public
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

内容的提问来源于stack exchange,提问作者Badar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 14:23:26