6

请参阅下面的更新

我用 Express 编写了一个 Node.js 应用程序,在本地运行良好,但是当我在 Heroku 上运行该应用程序时,它给了我以下错误:

2013-01-19T21:55:42+00:00 app[web.1]: module.js:340
2013-01-19T21:55:42+00:00 app[web.1]:     throw err;
2013-01-19T21:55:42+00:00 app[web.1]:     ^
2013-01-19T21:55:42+00:00 app[web.1]: Error: Cannot find module './blog/blog'
2013-01-19T21:55:42+00:00 app[web.1]:     at Function.Module._load (module.js:312:12)
2013-01-19T21:55:42+00:00 app[web.1]:     at Module.require (module.js:362:17)
2013-01-19T21:55:42+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:467:10)
2013-01-19T21:55:42+00:00 app[web.1]:     at require (module.js:378:17)
2013-01-19T21:55:42+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:15:12)
2013-01-19T21:55:42+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:338:15)
2013-01-19T21:55:42+00:00 app[web.1]:     at Module.load (module.js:356:32)
2013-01-19T21:55:42+00:00 app[web.1]:     at Module.runMain (module.js:492:10)
2013-01-19T21:55:42+00:00 app[web.1]:     at Function.Module._load (module.js:280:25)
2013-01-19T21:55:42+00:00 app[web.1]:     at Module._compile (module.js:449:26)
2013-01-19T21:55:43+00:00 heroku[web.1]: Process exited with status 1
2013-01-19T21:55:43+00:00 heroku[web.1]: State changed from starting to crashed

我不明白为什么它不能在 Heroku 上运行,因为完全相同的代码在本地完美运行。也许这与我如何将代码放在 Heroku 的服务器上有关?以防万一,下面是我的文件系统、我的文件的代码app.js以及我要加载的blog.js模块:app.js

文件系统:

文件系统

应用程序.js:

//requires and starts up app
var express = require('express');
var app = express();

//db setup
var mongoose = require('mongoose')
  , dbURI = 'localhost/brads-projects';

//configures app for production, connects to MongoHQ databse rather than localhost
app.configure('production', function () {
  dbURI = process.env.MONGOHQ_URL;
});

//requires the various project files
var blog = require('./blog/blog').blog;

//tries to connect to database.
mongoose.connect(dbURI);
//once connection to database is open, then rest of app runs
mongoose.connection.on('open', function () {
  //runs the blog app
  blog(app, express);

  app.listen(process.env.PORT || 5000);
});

//in the event of a connection to database error, the app will not run
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));

博客.js:

module.exports.blog = function(app, express) {
    //models
    var postmodel = require('./models/post').postmodel
      , usermodel = require('./models/user').usermodel
      , notificationmodel = require('./models/notification').notificationmodel
      , commentmodel = require('./models/comment').commentmodel;

    //controllers
    var indexHandler = require('./controllers/index').index
      , newpostHandler = require('./controllers/newpost').newpost
      , postandidHandler = require('./controllers/postandid').postandid
      , newPostHandler = require('./controllers/newpost').newpost
      , searchHandler = require('./controllers/search').postsearch
      , loginHandler = require('./controllers/login').login
      , logoutHandler = require('./controllers/login').logout
      , dashboardHandler = require('./controllers/dashboard').dashboard
      , registerHandler = require('./controllers/register').register
      , userSettingsHandler = require('./controllers/usersettings').usersettings
      , editpostHandler = require('./controllers/editpost').editpost
      , newCommentHandler = require('./controllers/newcomment').newcomment;

    //misc requires
    var MemStore = require('connect/lib/middleware/session/memory');

    //configures app for general stuff needed such as bodyParser and static file directory
    app.configure(function () {
        app.use(express.bodyParser());
        app.use(express.static(__dirname + '/static'));
        app.use(express.cookieParser('lockirlornie123'));
        app.use(express.session({store: MemStore( {
            reapInterval: 60000 * 10
        })}));
    });

    //requires a user session for access
    function requiresLogin(request, response, next) {
        if (request.session.user) {
            next();
        } else {
            response.redirect('/blog/login');
        }
    };

    //requires user session and admin for access
    function requiresLoginAndAdmin(request, response, next) {
        if (request.session.user && request.session.user.role === 'admin') {
            next();
        } else {
            if (request.session.user) {
                response.redirect('/blog');
            } else {
                response.redirect('/blog/login');
            }
        }
    };

    console.log("loaded");

    var PostModel = new postmodel();
    var Post = PostModel.setupPostSchema();

    var UserModel = new usermodel();
    var User = UserModel.setupUserSchema();

    var NotificationModel = new notificationmodel();
    var Notification = NotificationModel.setupNotificationSchema();
    NotificationModel.clickNotificationHandler(app, Notification);

    var CommentModel = new commentmodel();
    var Comment = CommentModel.setupCommentSchema();

    app.set('views', __dirname + '/views');
    app.set('view engine','jade');

    /*
    var newuser = new User({email: "brad.ross.35@gmail.com", password: UserModel.createHashPass("Brad1234"), role: 'admin', activated: true});
    newuser.save(function (err) {
        if (err) {
            console.log("error saving!");
        } else {
            console.log("successfully created!");
        }
    });
    */

    //get request for the home page that displays the 10 most recent posts
    indexHandler(app, Post, PostModel, NotificationModel.getNotifications, Notification);

    //get request for the unique page for every post
    postandidHandler(app, Post, NotificationModel.getNotifications, Notification, CommentModel.getComments, Comment);

    //post request for the submit url that creates a new post and puts it into the database
    //if a get request is sent to the sumbit page, it redirects users away from the /submit url in order to keep them away and not cause errors.
    newPostHandler(app, Post, requiresLogin, PostModel, NotificationModel.getNotifications, Notification);

    //post request to create a new comment
    newCommentHandler(app, Comment, requiresLogin, CommentModel, NotificationModel.getNotifications, Notification, NotificationModel.createNotification, Post);

    //get request for search page that both displays search results and allows users to create new search queries
    searchHandler(app, Post, NotificationModel.getNotifications, Notification);

    //login page get request and post request
    loginHandler(app, UserModel.authenticate, User);

    //logout page that redirects back to home
    logoutHandler(app);

    //dashboard page for managing posts by user
    //and if user is an admin, adding and deleting users
    dashboardHandler(app, User, Post, requiresLoginAndAdmin, NotificationModel.getNotifications, Notification, Comment);

    //a page for users to register for posting priveleges
    registerHandler(app, User, UserModel, NotificationModel.createNotification, Notification);

    //a page for user settings
    userSettingsHandler(app, User, UserModel, requiresLogin);

    //a page to edit posts
    editpostHandler(app, Post, requiresLogin, NotificationModel.getNotifications, Notification);
};

更新:

多亏了下面的建议,我已经跑去heroku run bash找出实际存在的文件,当我执行以下操作时,我发现了一些有趣的信息,即我尝试导入的文件实际上并不存在

~ $ cd ./blog
~/blog $ ls
~/blog $ cd ..
~ $ cd ./addressbook
~/addressbook $ ls
~/addressbook $ cd ..
~ $ cd ./views
~/views $ ls
addressbook  blog  index
~/views $ cd ./blog
~/views/blog $ ls
dashboard.jade  index.jade    layout.jade  newpost.jade    register.jade
editpost.jade   index_error.jade  login.jade   postandid.jade  search.jade

看起来我正在做的事情不是在 app/blog 和 app/addressbook 中上传这些文件。有趣和很好的信息。感谢您的建议...

4

2 回答 2

5

根据您提供的信息,这可能是答案。在 mac 终端中(我假设您正在运行 OSX 的屏幕截图)从 blog.js 文件所在的文件夹中运行此命令。

file blog.js -I

应该告诉您该文件的 MIME 类型为“text/plain”,如果返回的 MIME 类型为“text/xc”,那么该文件看起来像是最初在 Linux 上创建的 - 这是您的问题。

简单地解决这个问题:

  • 创建一个新文件
  • 复制 blog.js 的内容
  • git rm 旧文件
  • 重命名新文件
  • 获取添加新文件

新文件现在应该具有“text/plain”的 MIME 类型。将更改推送到 Heroku 并进行测试。

如果这不是问题,我的下一步将是运行:

heroku run bash

并查看该文件是否存在于您的应用程序期望在 Heroku 上找到它的位置。如果您仍有问题,请发回这些调查的结果。

让我知道!

参考:Mac 开发者库:'file' 命令

于 2013-01-22T21:37:54.277 回答
1

奇怪的行为;尝试使用以下方法进行调试:

console.log( __dirname );

确保以下路径正确(指向您的blog.js文件):

console.log( __dirname + '/blog/blog.js' );

然后尝试将其显式传递给require: (可能会因__dirname返回的内容而异..)

var blog = require( __dirname + '/blog/blog' ).blog;
于 2013-01-25T14:13:47.197 回答