从部署的 heroku 应用程序连接到 Mongolab 时 ECONNREFUSED
ECONNREFUSED when connecting to Mongolab from a deployed heroku app
我使用 angular-fullstack 生成器开发了一个网页,并尝试按照生成器 github 页面中列出的说明将其部署到 Heroku。
我添加了 mongolab 附加组件并重新启动了服务器,但在重新启动后我总是得到以下日志行:
2016-05-02T17:16:57.742152+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-02T17:17:01.929260+00:00 heroku[web.1]: Starting process with command node server/app.js
2016-05-02T17:17:06.396597+00:00 app[web.1]: Express server listening on 41865, in production mode
2016-05-02T17:17:06.405342+00:00 app[web.1]: MongoDB connection error: MongoError: connect ECONNREFUSED 127.0.0.1:27017
2016-05-02T17:17:07.516482+00:00 heroku[web.1]: Process exited with status 255
2016-05-02T17:17:07.525635+00:00 heroku[web.1]: State changed from starting to crashed
所以我四处寻找类似的问题,但大多数人都谈到在我已经拥有的配置中包括 process.env.MONGOLAB_URI 属性。顺便说一下,这些是我在 server/config:
下的配置
server/config/环境/index.js
'use strict';
var path = require('path');
var _ = require('lodash');
function requiredProcessEnv(name) {
if (!process.env[name]) {
throw new Error('You must set the ' + name + ' environment variable');
}
return process.env[name];
}
// All configurations will extend these options
// ============================================
var all = {
env: process.env.NODE_ENV,
// Root path of server
root: path.normalize(__dirname + '/../../..'),
// Server port
port: process.env.PORT || 9000,
// Server IP
ip: process.env.IP || '0.0.0.0',
// Should we populate the DB with sample data?
seedDB: false,
// Secret for session, you will want to change this and make it an environment variable
secrets: {
session: 'copaamerica-secret'
},
// MongoDB connection options
mongo: {
options: {
db: {
safe: true
}
}
},
facebook: {
clientID: process.env.FACEBOOK_ID || 'id',
clientSecret: process.env.FACEBOOK_SECRET || 'secret',
callbackURL: (process.env.DOMAIN || '') + '/auth/facebook/callback'
},
twitter: {
clientID: process.env.TWITTER_ID || 'id',
clientSecret: process.env.TWITTER_SECRET || 'secret',
callbackURL: (process.env.DOMAIN || '') + '/auth/twitter/callback'
},
google: {
clientID: process.env.GOOGLE_ID || 'id',
clientSecret: process.env.GOOGLE_SECRET || 'secret',
callbackURL: (process.env.DOMAIN || '') + '/auth/google/callback'
}
};
// Export the config object based on the NODE_ENV
// ==============================================
module.exports = _.merge(
all,
require('./shared'),
require('./' + process.env.NODE_ENV + '.js') || {});
server/config/环境/production.js
'use strict';
// Production specific configuration
// =================================
module.exports = {
// Server IP
ip: process.env.OPENSHIFT_NODEJS_IP ||
process.env.IP ||
undefined,
// Server port
port: process.env.OPENSHIFT_NODEJS_PORT ||
process.env.PORT ||
8080,
// MongoDB connection options
mongo: {
uri: process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
process.env.OPENSHIFT_MONGODB_DB_URL +
process.env.OPENSHIFT_APP_NAME ||
'mongodb://localhost/copaamerica'
}
};
和
server/config/express.js
/**
* Express configuration
*/
'use strict';
import express from 'express';
import favicon from 'serve-favicon';
import morgan from 'morgan';
import compression from 'compression';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import cookieParser from 'cookie-parser';
import errorHandler from 'errorhandler';
import path from 'path';
import lusca from 'lusca';
import config from './environment';
import passport from 'passport';
import session from 'express-session';
import connectMongo from 'connect-mongo';
import mongoose from 'mongoose';
var MongoStore = connectMongo(session);
export default function(app) {
var env = app.get('env');
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
// Persist sessions with MongoStore / sequelizeStore
// We need to enable sessions for passport-twitter because it's an
// oauth 1.0 strategy, and Lusca depends on sessions
app.use(session({
secret: config.secrets.session,
saveUninitialized: true,
resave: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
db: process.env.MONGOLAB_URI
})
}));
/**
* Lusca - express server security
* https://github.com/krakenjs/lusca
*/
if ('test' !== env) {
app.use(lusca({
csrf: {
angular: true
},
xframe: 'SAMEORIGIN',
hsts: {
maxAge: 31536000, //1 year, in seconds
includeSubDomains: true,
preload: true
},
xssProtection: true
}));
}
app.set('appPath', path.join(config.root, 'client'));
if ('production' === env) {
app.use(favicon(path.join(config.root, 'client', 'favicon.ico')));
app.use(express.static(app.get('appPath')));
app.use(morgan('dev'));
}
if ('development' === env) {
app.use(require('connect-livereload')());
}
if ('development' === env || 'test' === env) {
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(app.get('appPath')));
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
}
任何帮助都会很棒,我提前感谢大家!
没有深入研究,我的直接想法是您是否设置了 CORS。尝试执行 npm install cors
var express = require('express')
, cors = require('cors')
, app = express();
app.use(cors());
app.get('/products/:id', function(req, res, next){
res.json({msg: 'This is CORS-enabled for all origins!'});
});
app.listen(80, function(){
console.log('CORS-enabled web server listening on port 80');
});
好的,我发现问题出在哪里了。 heroku 为数据库连接设置的环境变量称为 MONGODB_URI,但 angular-fullstack 应用程序需要一个 MONGOLAB_URI。在 MONGOLAB_URI 所在的位置添加 process.env.MONGODB_URI 就可以了。
我使用 angular-fullstack 生成器开发了一个网页,并尝试按照生成器 github 页面中列出的说明将其部署到 Heroku。
我添加了 mongolab 附加组件并重新启动了服务器,但在重新启动后我总是得到以下日志行:
2016-05-02T17:16:57.742152+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-02T17:17:01.929260+00:00 heroku[web.1]: Starting process with command
node server/app.js
2016-05-02T17:17:06.396597+00:00 app[web.1]: Express server listening on 41865, in production mode
2016-05-02T17:17:06.405342+00:00 app[web.1]: MongoDB connection error: MongoError: connect ECONNREFUSED 127.0.0.1:27017
2016-05-02T17:17:07.516482+00:00 heroku[web.1]: Process exited with status 255
2016-05-02T17:17:07.525635+00:00 heroku[web.1]: State changed from starting to crashed
所以我四处寻找类似的问题,但大多数人都谈到在我已经拥有的配置中包括 process.env.MONGOLAB_URI 属性。顺便说一下,这些是我在 server/config:
下的配置server/config/环境/index.js
'use strict';
var path = require('path');
var _ = require('lodash');
function requiredProcessEnv(name) {
if (!process.env[name]) {
throw new Error('You must set the ' + name + ' environment variable');
}
return process.env[name];
}
// All configurations will extend these options
// ============================================
var all = {
env: process.env.NODE_ENV,
// Root path of server
root: path.normalize(__dirname + '/../../..'),
// Server port
port: process.env.PORT || 9000,
// Server IP
ip: process.env.IP || '0.0.0.0',
// Should we populate the DB with sample data?
seedDB: false,
// Secret for session, you will want to change this and make it an environment variable
secrets: {
session: 'copaamerica-secret'
},
// MongoDB connection options
mongo: {
options: {
db: {
safe: true
}
}
},
facebook: {
clientID: process.env.FACEBOOK_ID || 'id',
clientSecret: process.env.FACEBOOK_SECRET || 'secret',
callbackURL: (process.env.DOMAIN || '') + '/auth/facebook/callback'
},
twitter: {
clientID: process.env.TWITTER_ID || 'id',
clientSecret: process.env.TWITTER_SECRET || 'secret',
callbackURL: (process.env.DOMAIN || '') + '/auth/twitter/callback'
},
google: {
clientID: process.env.GOOGLE_ID || 'id',
clientSecret: process.env.GOOGLE_SECRET || 'secret',
callbackURL: (process.env.DOMAIN || '') + '/auth/google/callback'
}
};
// Export the config object based on the NODE_ENV
// ==============================================
module.exports = _.merge(
all,
require('./shared'),
require('./' + process.env.NODE_ENV + '.js') || {});
server/config/环境/production.js
'use strict';
// Production specific configuration
// =================================
module.exports = {
// Server IP
ip: process.env.OPENSHIFT_NODEJS_IP ||
process.env.IP ||
undefined,
// Server port
port: process.env.OPENSHIFT_NODEJS_PORT ||
process.env.PORT ||
8080,
// MongoDB connection options
mongo: {
uri: process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
process.env.OPENSHIFT_MONGODB_DB_URL +
process.env.OPENSHIFT_APP_NAME ||
'mongodb://localhost/copaamerica'
}
};
和 server/config/express.js
/**
* Express configuration
*/
'use strict';
import express from 'express';
import favicon from 'serve-favicon';
import morgan from 'morgan';
import compression from 'compression';
import bodyParser from 'body-parser';
import methodOverride from 'method-override';
import cookieParser from 'cookie-parser';
import errorHandler from 'errorhandler';
import path from 'path';
import lusca from 'lusca';
import config from './environment';
import passport from 'passport';
import session from 'express-session';
import connectMongo from 'connect-mongo';
import mongoose from 'mongoose';
var MongoStore = connectMongo(session);
export default function(app) {
var env = app.get('env');
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
// Persist sessions with MongoStore / sequelizeStore
// We need to enable sessions for passport-twitter because it's an
// oauth 1.0 strategy, and Lusca depends on sessions
app.use(session({
secret: config.secrets.session,
saveUninitialized: true,
resave: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
db: process.env.MONGOLAB_URI
})
}));
/**
* Lusca - express server security
* https://github.com/krakenjs/lusca
*/
if ('test' !== env) {
app.use(lusca({
csrf: {
angular: true
},
xframe: 'SAMEORIGIN',
hsts: {
maxAge: 31536000, //1 year, in seconds
includeSubDomains: true,
preload: true
},
xssProtection: true
}));
}
app.set('appPath', path.join(config.root, 'client'));
if ('production' === env) {
app.use(favicon(path.join(config.root, 'client', 'favicon.ico')));
app.use(express.static(app.get('appPath')));
app.use(morgan('dev'));
}
if ('development' === env) {
app.use(require('connect-livereload')());
}
if ('development' === env || 'test' === env) {
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(app.get('appPath')));
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
}
任何帮助都会很棒,我提前感谢大家!
没有深入研究,我的直接想法是您是否设置了 CORS。尝试执行 npm install cors
var express = require('express')
, cors = require('cors')
, app = express();
app.use(cors());
app.get('/products/:id', function(req, res, next){
res.json({msg: 'This is CORS-enabled for all origins!'});
});
app.listen(80, function(){
console.log('CORS-enabled web server listening on port 80');
});
好的,我发现问题出在哪里了。 heroku 为数据库连接设置的环境变量称为 MONGODB_URI,但 angular-fullstack 应用程序需要一个 MONGOLAB_URI。在 MONGOLAB_URI 所在的位置添加 process.env.MONGODB_URI 就可以了。