0

所以在尝试了很多事情之后,我已经使用 Heroku 连接到我的 MongoLab 服务器。唯一的问题是在我真正去我的网站之前,它给了我一个Process exited with status 1(下面的完整日志)。该应用程序在我的本地服务器上运行良好。下面是 app.js 代码。该应用程序本身位于 brads-testing.herokuapp.com。

应用程序.js

//requires and start up app
var express = require('express');
var mongoose = require('mongoose')
  , dbURI = 'localhost/test';
var app = express();

//configures app for production, connects to mongoLab databse rather than localhost
app.configure('production', function () {
    //app.use(express.logger());
    console.log("production!");
    dbURI = 'mongodb://USERNAME:PASSWORD@ds037387.mongolab.com:37387/test';
});

//configures app for general stuff needed
app.configure(function () {
    //app.use(express.logger());
    app.use(express.bodyParser());
    app.use(express.static(__dirname + '/static'));
});

//tries to connect to database. If it does, connected set to true. If not, connected stays false
var connected = false;
mongoose.connect(dbURI);
mongoose.connection.on('open', function () {
    console.log("connected!");
    connected = true;
    //creates new schema for object post and saves it to the database
    var postSchema = new mongoose.Schema({
        body: String
    });

    var Post = mongoose.model('Post', postSchema);

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

    app.get('/', function(request, response) {
        response.render('index');
    });

    app.post('/result', function(request, response) {
    if (connected) {
        var post = new Post({body: request.body.text});
        console.log(post);
        post.save(function (err) {
            if (err) {
                console.log("error!");
            } else {
                console.log("saved!");
            }
        });

        Post.find(function (err, posts) {
            if (!err) {
                console.log("found!");
                console.log(posts);
                response.render('result', {posts: posts});
            } else {
                connected = false;
                var error = "The server had an issue sending/executing the query..."
                response.render('result_error', {error: error});
            }
        });
        } else {
        var error = "The server didn't end up connecting..."
        response.render('result_error', {error: error});
        }
    });

    app.get('/result', function (request, response) {
        if (connected) {
        Post.find(function (err, posts) {
            if (!err) {
                console.log("found!");
                console.log(posts);
                response.render('result', {posts: posts});
            } else {
                connected = false;
                var error = "The server had an issue sending/executing the query..."
                response.render('result_error', {error: error});
            }
        });
        } else {
            var errorText = "The server didn't end up connecting..."
            response.render('result_error', {error: errorText});
        }
    });

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

});
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));

终端日志

2012-08-22T19:19:26+00:00 heroku[web.1]: Starting process with command `node app.js`
2012-08-22T19:19:26+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2012-08-22T19:19:28+00:00 app[web.1]: production!
2012-08-22T19:19:28+00:00 app[web.1]: connected!
2012-08-22T19:19:28+00:00 heroku[web.1]: State changed from starting to up
2012-08-22T19:19:29+00:00 heroku[web.1]: Process exited with status 1

可悲的是,connected!信息和信息Process exited with status 1是可以互换的。有时一个在另一个之前,有时它是相反的。希望有人可以提供帮助。

4

1 回答 1

0

获得更多日志信息会很好。我代替它,这里有几件事可以在你自己的环境中尝试:

  • 已知某些驱动程序不适用于所有版本的 MongoDB。MongoLab 正在运行 2.0.x,这是目前最新的稳定代码线。但由于我们正处于 2.2.x GA 版本的风口浪尖上,所以有一些驱动程序,特别是一些驱动程序版本,需要最新的 2.2.x 开发版本。我不确定 Mongoose 是否是其中之一,但您应该尝试在本地运行与您的 MongoLab 实例正在运行的版本相同的版本。

  • MongoLab 数据库以身份验证模式运行,并且用户仅被授予对他们自己的数据库的权限(出于明显的安全原因)。这意味着某些命令(例如列出所有数据库)将失败。因此,请尝试以身份验证模式运行本地数据库并以非管理员用户身份进行身份验证。

于 2012-08-23T01:49:25.250 回答