Heroku (MERN) 中没有指定默认引擎,也没有提供扩展

No default engine was specified and no extension was provided in Heroku (MERN)

所以我得到了我的 MERN 应用程序,我想在 Heroku 中上传它。

一切正常,直到我访问我的 Heroku 应用 URL 并且在日志中,我收到此错误:No default engine was specified and no extension was provided

我会使用 app.set('view engine', 'ejs'); 但我正在使用 React - MERN。

节点:

const express = require('express');
const logger = require('morgan');
const passport = require('./passport/');
const PORT = process.env.PORT || 8080;
const app = express();
const mongoose = require('mongoose');
mongoose.connect('mongodb', {useNewUrlParser: true})
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
app.use(passport.initialize());
app.use(passport.session());
app.use(session({
    resave: false,
    saveUninitialized: false,
    secret: 'secret here',
    store: new MongoStore({ mongooseConnection: mongoose.connection })
}))

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.use('/', indexRouter);
app.use('/authentication', usersRouter);

if (process.env.NODE_ENV === 'production') {
    app.use(express.static('client/build'))

    app.get("*", (req, res) => {
        res.sendFile(path.resolve(__dirname, "../client", "build", "index.html"))
    })
}


app.listen(PORT);

module.exports = app

package.json:

{
  "name": "name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon app.js",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  }
  }
}

有谁知道如何解决这个问题?

-非常感谢

也许可以尝试 res.sendFile(path.resolve(__dirname, "client", "build", "index.html")) 因为使用 __dirname 会转到主目录

我认为你需要像这样在 package.json 上写它:

{
  "name": "name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": {
    "node": "12.13.0",
    "npm": "6.14.4"
  },
  "scripts": {
    "start": "nodemon app.js",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  }
}

Replace node and npm with what you need and the same version you have in local.