因此,在嘗試了一大堆事情之後,我使用Heroku與MongoLab服務器建立了連接。唯一的問題是我真的去我的網站之前,它給了我一個Process exited with status 1
(下面的完整日誌)。該應用程序在我的本地服務器上運行得很好。以下是app.js代碼。該應用程序本身在brads-testing.herokuapp.com。Express Node.js應用程序使用Mongoose丟失與MongoLab數據庫的連接
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:[email protected]: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
是種可互換的。有時候一個先到另一個,有時候卻是相反的。希望有人能幫助。
你想要什麼樣的日誌信息?本地我運行MongoDB 2.0.7,並且在本地工作正常。我不知道在哪裏尋找Mongoose對MongoDB版本的依賴。關於auth模式,我只是建立了一個與'test'數據庫的連接(據我所知),所以它不應該成爲一個問題。我如何在auth模式下在本地運行我的數據庫? –
以下是[在auth模式下運行]的MongoDB文檔(http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-RunninginSecureMode%28with%5Cauthor%5CkeyFile%29)。我沒有關於啓用日誌記錄的任何具體建議,但無論調用具有非零值的exit都應該提供更多的信息來提供原因 - 未處理的異常或錯誤代碼。 – jared
是啊,你對快遞知道多少?我應該啓用'express.logger()'並看看我得到了什麼?另外,在我運行'mongod'命令之前,我無法訪問mongodb localhost數據庫,但是當我這樣做時,我無法訪問shell直到我停止它。 –