简体   繁体   English

next.js 应用程序在本地运行良好,但在生产中返回 500 内部服务器错误

[英]next.js app works fine in local but returns 500 internal server error on production

the problem i'm having is basically my app works fine in local but in production anywhere that i've used server side rendering returns 500 internal server error.我遇到的问题基本上是我的应用程序在本地运行良好,但在我使用服务器端渲染的任何地方的生产中都返回 500 内部服务器错误。 the other parts of my site which are called normally like in useEffect or componentDidMount work completely fine, like my dashboard or authorization process works without a problem, but anywhere that i have used ssr returns 500. Below is some examples of how i have handled my ssr pages.我网站的其他部分通常像useEffectcomponentDidMount工作得很好,就像我的仪表板或授权过程没有问题一样,但我使用过ssr任何地方都返回 500。下面是我如何处理我的一些示例ssr页面。

index page:索引页:

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

here is my Api component:这是我的Api组件:

import axios from 'axios';

const GuestHeaders = {
    'Authorization': "",
    'content-type': 'application/json'
}

const Api = axios.create({
    baseURL: 'baseUrl'
})

export { Api, GuestHeaders };

here is my server.js :这是我的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')
  })
})

and my next.config.js :和我的next.config.js

module.exports = {
    basePath: '',
    trailingSlash: false,
}

所以,这个问题有一个更简单的答案,那就是如果你想太简单地了解这个错误发生的原因,你应该只将cpanel的应用程序模式放在开发中,这样,它就会显示错误的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM