首页 > 解决方案 > 我的反应应用程序在本地工作,但无法在 heroku 上部署

问题描述

当我 npm run start 时在本地工作,但在 heroku 上失败。我在日志上得到的唯一错误是“此应用程序可能未指定任何启动节点进程的方式”。我尝试更改脚本。我尝试使用创建反应应用程序文档。我还为项目实施了“https://github.com/mars/create-react-app-buildpack.git”构建包

我的文件结构

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

我的主要 package.json 脚本

"scripts": {
    "start": "npm run start:dev",
    "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": "npm run build"
  },

我的 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(path.join(__dirname, '../client/build')));

// 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}`);
});

在此处输入图像描述

标签: javascriptnode.jsreactjsherokudeployment

解决方案


好像您还没有添加 Procfile。如果不在您的节点 API 的根目录中,请添加一个名为 Procfile 且没有扩展名的文件,即 .js。制作文件后,添加以下行:web: npm run start。


推荐阅读