【问题标题】:having trouble deploying react app with mongo db to heroku使用 mongodb 将 react 应用程序部署到 heroku 时遇到问题
【发布时间】:2021-02-26 06:25:38
【问题描述】:

我有一个 react 应用程序,当我执行 npm run start 时可以在本地运行,但无法在 heroku 上部署,我得到的唯一错误是“此应用程序可能未指定任何启动节点进程的方式”

这是目前我的 package.json 脚本

    "start": "react-scripts start",
    "start:prod": "node server/server.js",
    "start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
    "client": "cd client && npm run start",
    "seed": "node server/scripts/seedDB.js",
    "install": "cd client && npm install",
    "build": "cd client && npm run build",
    "heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
  },

还有我的文件构造器

//client
   |
   +-node modules
   + public
   +-src
   +-package-lock
   +-package.json
        
//node_modules
//server
    |
    +-models
    +-server.js
    +-routes
    +-middlewares
    +-config
+-package-lock.json
+-package.json

这是我的 server.js

require('dotenv').config();

const express = require('express');
const morgan = require('morgan');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const dbConnection = require('./config/connection');
const passport = require('./config/passport');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 8090;

// Middlewares
app.use(morgan('dev'));
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(express.static('client/build'));

app.get('*', (req, res) => {
  return res.sendFile(path.resolve('client', 'build', 'index.html'));
});

// Passport
app.use(passport.initialize());
app.use(passport.session()); // will call the deserializeUser
app.use(session({
  secret: 'my_secret', // process.env.AUTH_SECRET,
  store: new MongoStore({ mongooseConnection: dbConnection }),
  resave: false,
  saveUninitialized: false
}));

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

// Add Auth and API routes
app.use('/auth', require('./routes/authRoutes'));
app.use('/api', require('./routes/apiRoutes'));

// If no routes are hit, send the React app
app.use(function(req, res) {
  res.sendFile(path.join(__dirname, '../../client/build/index.html'));
});

// Error handler
app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500);
})

app.listen(PORT, () => {
    console.log(`App listening on PORT: ${PORT}`);
});

然后我在heroku上遇到的错误

此应用可能未指定任何方式来启动节点进程

【问题讨论】:

    标签: node.js reactjs heroku deployment mern


    【解决方案1】:

    您需要将 react static 文件与节点服务器一起提供,以实现您想要做的事情。 将此添加到您的server.js

    app.use(express.static('client/build'));
    
    app.get('*', (req, res) => {
      return res.sendFile(path.resolve('client', 'build', 'index.html'));
    });
    

    【讨论】:

    • 这将有助于更好地了解您面临的问题。
    • 添加了我的应用js
    猜你喜欢
    • 2021-04-21
    • 2016-08-27
    • 2017-05-03
    • 2011-08-30
    • 2019-02-18
    • 1970-01-01
    • 2021-07-21
    • 2021-11-08
    • 2017-01-18
    相关资源
    最近更新 更多