【发布时间】:2021-09-25 16:15:40
【问题描述】:
我正在尝试托管一个 NextJS 应用程序,并且在本地似乎一切正常。我能够从该站点获取数据,并且可以访问该站点并查看返回的原始 json,但是当我尝试在生产环境中运行时,API 完全无法通过浏览器和 Axios 请求访问.
服务器只返回500 或Internal Server Error。
我尝试在 DigitalOcean App Platform 和 AWS Amplify 上进行部署,但都无法连接到 API 路由。
我关注了this tutorial 的 NextJS SSR 方法,该方法说要构建并开始使用
// next.config.js
const path = require('path')
const Dotenv = require('dotenv-webpack')
require('dotenv').config()
module.exports = {
webpack: (config) => {
config.plugins = config.plugins || []
config.module.rules.push({
test: /\.svg$/,
use: ["@svgr/webpack"]
});
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]
return config
},
sassOptions: {
includePaths: [path.join(__dirname, 'styles')]
}
}
// package.json
...
"scripts": {
"dev": "next dev",
"build": "next build",
"digitalocean": "next start -H 0.0.0.0 -p ${PORT:-8080}",
"start": "next start"
},
...
// api.js
const axios = require('axios')
const {getS3URL} = require('./aws')
require('dotenv').config()
export default async (req, res) => {
const config = {
bucket: 'bucket',
key: 'folder/data.json'
}
const request = await axios.get(await getS3URL(config));
try {
res.status(200).json(JSON.stringify(request.data))
} catch {
res.status(500).json({ error: '500', response })
res.status(400).json({ error: '400', response })
}
}
// frontend.js
...
const getData = async () => {
console.log(`${host}api/daily-trip-stats`)
const trips = await axios.get(`${host}api/daily-trip-stats`)
const routes = await axios.get(`${host}api/daily-route-stats`)
const stops = await axios.get(`${host}api/daily-stops-routes`)
const cleanUp = async (data) => {
return await data.map(fea => fea.properties)
}
return {
routes: await cleanUp(routes.data.features),
trips: await cleanUp(trips.data.features),
stops: await cleanUp(stops.data.features)
}
};
...
【问题讨论】:
-
在生产环境中向 API 路由发出请求时可以查看服务器日志吗?此外,您可能希望在
try/catch块中包含axios.get调用,以便捕获它引发的任何潜在错误。 -
感谢提醒我有办法远程调试...
标签: amazon-s3 next.js digital-ocean aws-amplify