Next.js Netx.js返回500:生产中的内部服务器错误
已创建next.js完整堆栈应用程序。在生产构建之后,当我运行下一个start时,它返回500:internalserver。我使用环境变量来实现apiNext.js Netx.js返回500:生产中的内部服务器错误,next.js,next,Next.js,Next,已创建next.js完整堆栈应用程序。在生产构建之后,当我运行下一个start时,它返回500:internalserver。我使用环境变量来实现api env.development file BASE_URL=http://localhost:3000 它在开发中运行良好 服务台 import axios from 'axios'; const axiosDefaultConfig = { baseURL: process.env.BASE_URL, // is this line
env.development file
BASE_URL=http://localhost:3000
它在开发中运行良好
服务台
import axios from 'axios';
const axiosDefaultConfig = {
baseURL: process.env.BASE_URL, // is this line reason for error?
headers: {
'Access-Control-Allow-Origin': '*'
}
};
const axio = axios.create(axiosDefaultConfig);
export class Steam {
static getGames = async () => {
return await axio.get('/api/getAppList');
};
}
我认为你没有安装环境
您需要对其进行配置以使其正常工作。不用它就试试吧,它会很好用的 你有
next.config.js
文件吗
要向应用程序添加运行时配置,请打开next.config.js并添加publicRuntimeConfig和serverRuntimeConfig配置:
module.exports = {
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: 'secret',
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static',
},
}
要访问应用程序中的运行时配置,请使用next/config,如下所示:
import getConfig from 'next/config'
// Only holds serverRuntimeConfig and publicRuntimeConfig
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
// Will only be available on the server-side
console.log(serverRuntimeConfig.mySecret)
// Will be available on both server-side and client-side
console.log(publicRuntimeConfig.staticFolder)
function MyImage() {
return (
<div>
<img src={`${publicRuntimeConfig.staticFolder}/logo.png`} alt="logo" />
</div>
)
}
export default MyImage
从“next/config”导入getConfig
//仅保存serverRuntimeConfig和publicRuntimeConfig
const{serverRuntimeConfig,publicRuntimeConfig}=getConfig()
//将仅在服务器端可用
log(serverRuntimeConfig.mySecret)
//将在服务器端和客户端都可用
log(publicRuntimeConfig.staticFolder)
函数MyImage(){
返回(
)
}
导出默认MyImage
我希望这会有所帮助。您是否尝试过使用生产设置在本地运行它?使用
next build
和next start
(?)。如果执行SSR,在大多数情况下(使用fetch),需要绝对URL。