所以在尝试了很多事情之后,我已经使用 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
是可以互换的。有时一个在另一个之前,有时它是相反的。希望有人可以提供帮助。