【问题标题】:React content not found heroku deployment未找到反应内容 heroku 部署
【发布时间】:2019-06-20 08:29:46
【问题描述】:

我正在尝试部署 nodejs 并将应用程序反应到 Heroku。它在本地工作得很好,它确实为我的后端服务,但除了我的 api 端点之外,我在其他任何东西上都找不到。似乎它没有在客户端文件夹中运行构建脚本。谁能帮我找出哪里出错了?

server.js

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');

const projects = require('./routes/api/projects');

const app = express();

// Body parser middlewaree
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// DB Config
const db = require('./config/keys').mongoURI;

// Connect to DB
mongoose
.connect(
 db,
 { useNewUrlParser: true }
)
.then(() => console.log('mongoDB connected.'))
.catch(err => console.log(err));

// Setting up routes
app.use('/api/projects', projects);

// Serve static assets if in production
app.use(express.static(path.join(__dirname, '/client/build')));

app.get('*', (req, res) => {
res.sendfile(path.join((__dirname, '/client/build/index.html')));
});

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server up and running on ${port}`));

package.json

"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},

【问题讨论】:

  • 能否请您发布完整的 server.js 代码?
  • 抱歉,更新了

标签: node.js heroku create-react-app


【解决方案1】:

我已经使用以下代码成功部署到 Heroku(我刚刚回答了一个类似的问题 here):

app.use(express.static(path.join(__dirname, './client/build')))

app.get('*', function(_, res) {
  res.sendFile(path.join(__dirname, './client/build/index.html'), function(err) {
    if (err) {
      res.status(500).send(err)
    }
  })
})

另外,如果您尝试部署 create-react-app 应用程序,您可以使用this buildpack。 Buildpacks 是在部署应用程序时运行的脚本。它们用于为您的应用安装依赖项并配置您的环境。

您可以查看full code here 我通常如何部署 Heroku 应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 2021-05-06
    • 2017-01-20
    • 2014-07-16
    • 2021-04-07
    • 2019-07-14
    • 2020-12-18
    • 2017-07-11
    相关资源
    最近更新 更多