[英]next.js app works fine in local but returns 500 internal server error on production
我遇到的問題基本上是我的應用程序在本地運行良好,但在我使用服務器端渲染的任何地方的生產中都返回 500 內部服務器錯誤。 我網站的其他部分通常像useEffect
或componentDidMount
工作得很好,就像我的儀表板或授權過程沒有問題一樣,但我使用過ssr
任何地方都返回 500。下面是我如何處理我的一些示例ssr
頁面。
索引頁:
import React from 'react';
import HomePage from '../components/homePage/index'
import { Api, GuestHeaders } from '../components/config'
const Home = (props) => {
return <HomePage {...props} />
}
export async function getServerSideProps() {
const Response = await Api.get(`/v1/index`, { headers: GuestHeaders })
return {
props: {
Detail: Response.data,
}
}
}
export default Home
這是我的Api
組件:
import axios from 'axios';
const GuestHeaders = {
'Authorization': "",
'content-type': 'application/json'
}
const Api = axios.create({
baseURL: 'baseUrl'
})
export { Api, GuestHeaders };
這是我的server.js
:
// server.js
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
}).listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
和我的next.config.js
:
module.exports = {
basePath: '',
trailingSlash: false,
}
所以,這個問題有一個更簡單的答案,那就是如果你想太簡單地了解這個錯誤發生的原因,你應該只將cpanel
的應用程序模式放在開發中,這樣,它就會顯示錯誤的根源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.