javascript - 我的反应应用程序在本地工作,但无法在 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}`);
});
解决方案
好像您还没有添加 Procfile。如果不在您的节点 API 的根目录中,请添加一个名为 Procfile 且没有扩展名的文件,即 .js。制作文件后,添加以下行:web: npm run start。
推荐阅读
- tensorflow - 使用 MPI 的 Horovod,在 kubernetes 上
- python - Numpy 计算,寻找漂亮的代码
- python - Snakemake 使用输入文件名的一部分作为输出
- javascript - 在这段代码中,当我更改格式时,它会在 Set 函数内的 else 语句中引发该错误?
- python - 定义自定义 _repr_html_ 以抑制 Jupyter 笔记本中对象的输出
- r - 在 Shiny 中使用散点图调整日历
- c - 在 C 中解析文件时遇到问题
- reactjs - 如何使用 React Select 进行错误处理
- python - 为什么我的函数内部的变量即使是全局变量也没有定义?
- php - 为名称字段插入 php echo 值