我使用Netxjs构建了一个全栈应用程序,我从它创建了一个构建版本,并在Cpanel上上传了.next
文件夹。我创建了一个node app:
我的节点应用控制台截图:
我在节点env变量和next.config.js
中添加了env变量:next.config.js
编码:
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
env: {
dbName: "my_databaseName",
dbUserName: "my_databaseName",
dbPassword: "my_databasepassword",
dbHost: "localhost",
baseUrl: "https://my-website.com",
NEXTAUTH_URL: "https://my-website.com",
},
};
module.exports = nextConfig;
但在日志文件中,我得到了错误:FetchError: invalid json response body at http://localhost:3000/api/home reason: Unexpected token < in JSON at position 0
当我在getServerSideProps
函数中检查.next/server/pages/index.js
文件时:
const response = await fetch(`${"http:localhost:3000"}/api/home`);
但应该是:
const response = await fetch(`${process.env.baseUrl}/api/home`);
因为这个我得到了500错误的网页上我有HTTP请求
1条答案
按热度按时间piah890a1#
首先,请使用
.env
文件,而不是在next config
文件中添加vars。其次,当你想部署下一个应用程序时,你必须
ignore
这些文件夹和文件,然后你必须在主机上构建你的应用程序。.next
node_modules
package-lock.json
.env.local
上传
.next
文件夹将需要很长时间,也可能会崩溃您的应用程序.