重新加载时反应 Nodejs 页面为空白

时间:2021-07-08 09:41:38

标签: node.js reactjs react-router-dom production

我将我的第一个 MERN 项目投入生产。

问题是,例如在重新加载时,我会得到一个 cannot GET /profile

我读到它是因为 react-router-dom。

所以这是我的代码应该处理这个,但我不能成功:

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

我知道第一行工作正常,但第二行让一切都崩溃了。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对于那些需要的人,我只是忘记了```const path = require('path')。