将节点应用程序部署到heroku时出错

时间:2016-01-06 00:50:46

标签: node.js mongodb heroku mongoose

我对节点比较陌生,并且在将节点web应用程序部署到heroku时遇到了问题...这似乎与我在服务器文件中创建我的mongoose连接的方式有关。以下是我收到的错误日志:

2016-01-06T00:41:30.384170+00:00 heroku[web.1]: State changed from starting to crashed
2016-01-06T00:43:56.191644+00:00 heroku[web.1]: State changed from crashed to starting
2016-01-06T00:43:57.774259+00:00 heroku[web.1]: Starting process with command `node server/server.js`
2016-01-06T00:44:00.138974+00:00 app[web.1]: listening on 4568
2016-01-06T00:44:00.172982+00:00 app[web.1]: 
2016-01-06T00:44:00.172992+00:00 app[web.1]: /app/node_modules/mongoose/node_modules/mongodb/lib/server.js:236
2016-01-06T00:44:00.172994+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2016-01-06T00:44:00.172994+00:00 app[web.1]:                                       ^
2016-01-06T00:44:00.172995+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2016-01-06T00:44:00.172996+00:00 app[web.1]:     at Object.exports._errnoException (util.js:856:11)
2016-01-06T00:44:00.172997+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:879:20)
2016-01-06T00:44:00.172998+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2016-01-06T00:44:00.968046+00:00 heroku[web.1]: Process exited with status 1
2016-01-06T00:44:00.983259+00:00 heroku[web.1]: State changed from starting to crashed

这是我的服务器文件的shell:

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var entryController = require('./entries/entryController.js');

    var app = express();

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended:true}));
    app.use(express.static(__dirname + '/../client'));

    mongoose.connect('mongodb://localhost');

    console.log('listening on 4568');
    app.listen(4568);

非常感谢任何建议,谢谢!

1 个答案:

答案 0 :(得分:2)

您正在尝试连接到localhost:

mongoose.connect('mongodb://localhost');

Localhost意味着"这台计算机。"您的数据库未在Heroku上与节点应用程序在同一容器中运行,因此您需要连接到数据库实际托管的位置。

您可能需要阅读以下教程:

https://scotch.io/tutorials/use-mongodb-with-a-node-application-on-heroku