【问题标题】:Error deploying node app to heroku将节点应用程序部署到 heroku 时出错
【发布时间】:2016-04-09 23:58:43
【问题描述】:

我对 node 比较陌生,并且在将 node 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

这是我的服务器文件的外壳:

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);

任何建议将不胜感激,谢谢!

【问题讨论】:

    标签: node.js mongodb heroku mongoose


    【解决方案1】:

    您正在尝试连接到本地主机:

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

    本地主机的意思是“这台计算机”。您的数据库与您的节点应用程序不在 Heroku 上的同一容器中运行,因此您需要连接到您的数据库实际托管的位置。

    您可能想阅读以下教程:

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

    【讨论】:

    • 该链接所说的只是使用 MongoLab 附加配置变量来设置您的数据库 uri。您是否建议您将您的数据库凭据推送到某个地方?
    猜你喜欢
    • 2012-10-12
    • 2020-10-20
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 2020-07-16
    • 2015-09-22
    • 2017-09-14
    • 2015-12-09
    相关资源
    最近更新 更多