Express Node.js应用程序使用Mongoose失去与MongoLab数据库的连接

时间:2012-08-23 01:12:59

标签: node.js heroku express mongoose mlab

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

app.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有点可互换。有时候一个人来到另一个人之前,有时候是另一种方式。希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

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

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

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