【问题标题】:Backend not working on MERN app on heroku后端无法在 heroku 上的 MERN 应用程序上运行
【发布时间】:2021-11-23 08:19:31
【问题描述】:

我已经在heroku 上部署了我的mern 应用程序。我的前端正在显示,但我无法访问我的数据库,可能是我的服务器引起了问题。 我的服务器代码

const port = process.env.PORT || 5000;
// Database Connection
const CONNECTION_URL = "mongodb+srv://Toor_300:ARh3SwhrBl5Nvaur@cluster0.rudek.mongodb.net/courses_site"
mongoose.connect(CONNECTION_URL, { useNewUrlParser:true })
    .then(() => app.listen(port, () => console.log('Listening to Port')))
    .catch((err) => console.log(`Connection Error: ${error}`))

// Middlewares
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.urlencoded({ extended: true }))
app.use(cors())

// Heroku
if (process.env.NODE_ENV === 'production') {
    app.use(express.static(path.join(__dirname, 'client','build')))
    app.get('*', (req, res) => {
        res.sendFile(path.join(__dirname, 'client', 'build'))
    })
}

// Routes
app.use("/register", userRoute)
app.use("/admin", adminRoute)

我在 React 中的 Axios Api

import axios from 'axios'

const CONNECTION_URL = "http://localhost:5000/admin"

export const createUser = (user) => axios.post(CONNECTION_URL, user)

export const getUser = (user) => axios.get(`${CONNECTION_URL}/login/${user.email}&${user.password}`)

export const resetPassword = (email) => axios.get(`${CONNECTION_URL}/reset/${email}`)

export const updatePassword = (info) => axios.patch(`${CONNECTION_URL}/reset`, info)

我尝试了很多选项,但似乎都不起作用。我认为错误出在我的 axios 上。提供的 url 是本地的,如何使其动态化?

【问题讨论】:

  • 希望下面的博客对你有帮助

标签: node.js reactjs heroku axios


【解决方案1】:

https://coursework.vschool.io/deploying-mern-app-to-heroku/

我一直在关注这个博客来部署 MERN 应用程序,您必须主要关注服务器端的目录结构和 package.json 设置

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2015-11-30
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2018-12-03
    • 2015-04-12
    • 1970-01-01
    相关资源
    最近更新 更多