我有一个react应用程序,本地工作时,我做了一个npm运行启动,但随后未能部署到heroku,我得到的唯一错误是'这个应用程序可能没有指定任何方式来启动一个节点进程'
这是我当前的包。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
上出现错误此应用程序可能没有指定任何启动节点进程的方式
您需要在节点服务器上提供reactstatic文件,以实现您想要做的事情。将此添加到您的server.js
app.use(express.static('client/build'));
app.get('*', (req, res) => {
return res.sendFile(path.resolve('client', 'build', 'index.html'));
});