【问题标题】:ejs app works locally but doest work on Herokuejs 应用程序在本地工作,但在 Heroku 上不起作用
【发布时间】:2020-01-18 12:35:50
【问题描述】:

我在 heroku 上运行一个 .ejs 应用程序。它在本地运行良好,但在访问 Heroku 上的链接时出现 500 错误。基本上我确定它与我所有呈现的(req.render) 链接都有问题。

下面是我的app.js

// app.js
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const cors = require('cors');
const flash = require('express-flash');
let path = require('path');

// Imports routes for the products
const photoalbum = require('./routes/photoalbum.route');
const events = require('./routes/events.route');
const verse = require('./routes/verse.route');
const trackRoute = require('./routes/tracks.route');
const fileRoute = require('./routes/files.route');
const slideRoute = require('./routes/slides.route');
const galleryCountCountRoute = require('./routes/gallery_count.route');
const userRoute = require('./routes/user.route');
/////////////////////
const app = express();
/////////////////////
// Set up mongoose connection
const mongoose = require('mongoose');
let dev_db_url = 'mongodb://Trex_son:S*******.mlab.com:43254/photoalbumdb';
let mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });//mongoose.set('useFindAndModify', false);

mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));


module.exports = db;
module.exports = mongoose;
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended: false}));

app.use(bodyParser.json({limit: '10mb', extended: true}));
app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
app.use(cors());
app.use(session({secret: "S@****", resave: false, saveUninitialized:true, cookie: { maxAge: 60000 } }));
app.use(flash());

app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'Views')));
app.use('/event-calendar-module', express.static(path.join(__dirname, 'event-calendar-module')));


app.use('/photoalbums', photoalbum);
app.use('/events', events);
app.use('/verse', verse);
app.use('/tracks', trackRoute);
app.use('/files', fileRoute);
app.use('/slides', slideRoute);
app.use('/gallerycount', galleryCountCountRoute);
app.use('/user', userRoute);

//handle 400
app.use((req, res)=> {
    res.status(400);
    res.sendFile(path.resolve('./Views', 'not-found.html'));
});
//handlle 500
app.use((error, req, res, next)=> {
    res.status(500);
    res.sendFile(path.resolve('./Views', 'error-not-found.html'));
});

const port = process.env.PORT || 5000;
app.listen(port, () => {
    console.log('Server is up and running on port number ' + port);
});

当我尝试前往路线user 时出现问题,它包含我的大部分渲染文件,即 https://localhost:5000/user/forgot。这是我在 Heroku 上得到 5000 错误但在本地工作的地方。

这是我的user.route.js 代码:

router.get('/forgot', user_controller.user_get_forgot);

这是我的user.controller.js 代码:

exports.user_get_forgot = function(req, res) {
    res.render('forgot', {information: message});//, {User: req.user}
};

请帮助解决这个问题,如果需要其他任何内容,请告诉我。谢谢。

这是当时日志显示的内容:

2020-01-17T04:34:18.989581+00:00 heroku[router]: at=info method=GET path="/user/forgot" host=mfmsouth.herokuapp.com request_id=60316706-2a9a-44be-bf26-b1b6e476b7e7 fwd="67.167.63.7" dyno=web.1 connect=0ms service=4ms status=500 bytes=2436 protocol=http
2020-01-17T04:34:19.101234+00:00 heroku[router]: at=info method=GET path="/assets/login-logout/logout.js" host=mfmsouth.herokuapp.com request_id=4743ce6b-aef1-43ac-8590-109459382d57 fwd="67.167.63.7" dyno=web.1 connect=0ms service=3ms status=304 bytes=269 protocol=http
2020-01-17T04:34:19.055960+00:00 heroku[router]: at=info method=GET path="/assets/not-found/css/error-not-found.css" host=mfmsouth.herokuapp.com request_id=52edb65f-468f-438f-ae95-f7f5acb0d09b fwd="67.167.63.7" dyno=web.1 connect=0ms service=2ms status=304 bytes=269 protocol=http
2020-01-17T04:34:19.168099+00:00 heroku[router]: at=info method=GET path="/assets/not-found/js/error-not-found.js" host=mfmsouth.herokuapp.com request_id=19532d6b-3d76-4828-a4aa-2b1566492ae3 fwd="67.167.63.7" dyno=web.1 connect=0ms service=2ms status=304 bytes=268 protocol=http

使用git status --ignored 后,我在 CMD 中得到了这个日志:

C:\Users\Tozyne\Desktop\mfm - git>git status --ignored
Refresh index: 100% (1925/1925), done.
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .idea/workspace.xml
        modified:   Views/assets/index-additional/js/index-basic.js
        modified:   Views/index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Views/assets/images/slide/slides2020-01-12T19-52-54.296Z.jpg
        Views/assets/images/slide/slides2020-01-12T19-53-31.511Z.jpg
        Views/assets/index-additional/js/index-place-ratings.js
        node_modules/async/CHANGELOG.md
        node_modules/async/LICENSE
        node_modules/async/README.md
        node_modules/async/all.js
        node_modules/async/allLimit.js
        node_modules/async/allSeries.js
        node_modules/async/any.js
        node_modules/async/anyLimit.js
        node_modules/async/anySeries.js
        node_modules/async/apply.js
        node_modules/async/applyEach.js
        node_modules/async/applyEachSeries.js
        node_modules/async/asyncify.js
        node_modules/async/auto.js
        node_modules/async/autoInject.js
        node_modules/async/bower.json
        node_modules/async/cargo.js
        node_modules/async/cargoQueue.js
        node_modules/async/compose.js
        node_modules/async/concat.js
        node_modules/async/concatLimit.js
        node_modules/async/concatSeries.js
        node_modules/async/constant.js
        node_modules/async/detect.js
        node_modules/async/detectLimit.js
        node_modules/async/detectSeries.js
        node_modules/async/dir.js
        node_modules/async/dist/
        node_modules/async/doDuring.js
        node_modules/async/doUntil.js
        node_modules/async/doWhilst.js
        node_modules/async/each.js
        node_modules/async/eachOf.js
        node_modules/async/eachOfLimit.js
        node_modules/async/eachOfSeries.js
        node_modules/async/ensureAsync.js
        node_modules/async/every.js
        node_modules/async/everyLimit.js
        node_modules/async/everySeries.js
        node_modules/async/filter.js
        node_modules/async/filterSeries.js
        node_modules/async/find.js
        node_modules/async/findLimit.js
        node_modules/async/findSeries.js
        node_modules/async/flatMap.js
        node_modules/async/flatMapLimit.js
        node_modules/async/flatMapSeries.js
        node_modules/async/foldl.js
        node_modules/async/foldr.js
        node_modules/async/forEach.js
        node_modules/async/forEachLimit.js
        node_modules/async/forEachOfLimit.js
        node_modules/async/forEachOfSeries.js
        node_modules/async/forEachSeries.js
        node_modules/async/groupBy.js
        node_modules/async/groupByLimit.js
        node_modules/async/groupBySeries.js
        node_modules/async/index.js
        node_modules/async/inject.js
        node_modules/async/internal/DoublyLinkedList.js
        node_modules/async/internal/Heap.js
        node_modules/async/internal/asyncEachOfLimit.js
        node_modules/async/internal/awaitify.js
        node_modules/async/internal/breakLoop.js
        node_modules/async/internal/consoleFunc.js
        node_modules/async/internal/createTester.js
        node_modules/async/internal/eachOfLimit.js
        node_modules/async/internal/filter.js
        node_modules/async/internal/getIterator.js
        node_modules/async/internal/initialParams.js
        node_modules/async/internal/isArrayLike.js
        node_modules/async/internal/iterator.js
        node_modules/async/internal/map.js
        node_modules/async/internal/onlyOnce.js
        node_modules/async/internal/parallel.js
        node_modules/async/internal/promiseCallback.js
        node_modules/async/internal/range.js
        node_modules/async/internal/setImmediate.js
        node_modules/async/internal/withoutIndex.js
        node_modules/async/internal/wrapAsync.js
        node_modules/async/log.js
        node_modules/async/map.js
        node_modules/async/mapLimit.js
        node_modules/async/mapSeries.js
        node_modules/async/mapValuesSeries.js
        node_modules/async/memoize.js
        node_modules/async/nextTick.js
        node_modules/async/package.json
        node_modules/async/parallel.js
        node_modules/async/parallelLimit.js
        node_modules/async/priorityQueue.js
        node_modules/async/queue.js
        node_modules/async/race.js
        node_modules/async/reduce.js
        node_modules/async/reduceRight.js
        node_modules/async/reflect.js
        node_modules/async/reject.js
        node_modules/async/rejectLimit.js
        node_modules/async/rejectSeries.js
        node_modules/async/retryable.js
        node_modules/async/selectLimit.js
        node_modules/async/seq.js
        node_modules/async/setImmediate.js
        node_modules/async/some.js
        node_modules/async/someLimit.js
        node_modules/async/someSeries.js
        node_modules/async/sortBy.js
        node_modules/async/times.js
        node_modules/async/timesSeries.js
        node_modules/async/transform.js
        node_modules/async/tryEach.js
        node_modules/async/unmemoize.js
        node_modules/async/until.js
        node_modules/async/waterfall.js
        node_modules/async/wrapSync.js
        node_modules/connect-flash/
        node_modules/ejs/
        node_modules/express-flash/
        node_modules/nodemailer/

我看到它没有添加node_modules/ejs/

【问题讨论】:

  • 你能把heroku的错误日志贴在这里吗?
  • @Sarfraaz:我刚刚编辑了我的问题并将其添加到其中。
  • 检查你的views文件夹的名字是否正确&它没有被添加到.gitignore中
  • @Sarfraaz:我从来没有新的关于那个大声笑,我只是用git status --ignored检查过它,这就是我发现我会把它添加到上面的问题中。我注意到它没有添加很多东西,包括我的 ejs 视图文件夹,我该如何解决这个问题。谢谢。
  • 解析忽略的文件后,我意识到这与视图引擎没有显示任何 .ejs 文件有关。我被这个困住了。

标签: node.js express heroku ejs


【解决方案1】:

难以置信,花了将近 18 个小时来研究解决方案并在这里等待答案,叹息!!!,我从来没有得到,我终于能够弄清楚了。 我添加了这行代码:

app.set('views', path.join(__dirname, './Views'));

经过这么多变通办法,这最终使它工作。我绑定了app.set('views', path.join(__dirname, '../Views')app.set('views', path.join(__dirname, 'Views')app.set('views', path.join(__dirname, '../../Views)。没有工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多