【发布时间】: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