【问题标题】:No default engine was specified and no extension was provided in Heroku (MERN)Heroku(MERN)中没有指定默认引擎,也没有提供扩展
【发布时间】:2020-05-07 17:26:58
【问题描述】:

所以我得到了我的 MERN 应用,我想将它上传到 Heroku。

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

我会使用 app.set('view engine', 'ejs');,但我使用的是 react - MERN。

Nodejs:

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"
  }
  }
}

有人知道怎么解决吗?

-非常感谢

【问题讨论】:

  • 显示你的nodejs服务器代码
  • 完成感谢您的帮助
  • 哦,我有一个上传时间冷却,无法上传代码:(
  • res.sendFile(path.resolve(__dirname, "../client", "build", "index.html"))语句的错误吗?
  • 什么意思?

标签: reactjs heroku deployment internal-server-error mern


【解决方案1】:

我认为你需要像这样在你的 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.

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 好的,我会尽力告诉你的!
    • 显示你的 package.json
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2014-04-18
    相关资源
    最近更新 更多