I have a simple node with mongo (via mongojs) app that is developed locally and deployed on heroku. In my development environment, i want to use a local instance of mongo, while in production, I would like to use the instance heroku provides to me via "process.env.MONGOLAB_URI".
我有一个简单的节点与mongo(通过mongojs)应用程序,本地开发并部署在heroku上。在我的开发环境中,我想使用mongo的本地实例,而在生产中,我想通过“process.env.MONGOLAB_URI”使用heroku提供给我的实例。
My current approach is that I would set the datavase url depending on the environment variable, but how do i actually go into production mode? Moreover, how can i configure this so that when i develop on my local machine its development mode, when i upload to heroku its production mode?
我目前的方法是根据环境变量设置datavase url,但我如何实际进入生产模式?此外,我如何配置这个,以便当我在我的本地机器上开发其开发模式时,当我上传到heroku其生产模式?
app.configure('production', function(){
// ...
databaseUrl = "mydb"; // the default
});
app.configure('development', function(){
// ...
databaseUrl = process.env.MONGOLAB_URI;
});
db = require("mongojs").connect(databaseUrl);
2 个解决方案
#1
8
Set the NODE_ENV environment variable to "development" on your local environment, and set it to "production" on Heroku. https://devcenter.heroku.com/articles/nodejs#setting-node-env
在本地环境中将NODE_ENV环境变量设置为“development”,并在Heroku上将其设置为“production”。 https://devcenter.heroku.com/articles/nodejs#setting-node-env
#2
0
You can also access your online database locally by starting your app by adding the following:
您还可以通过添加以下内容启动应用程序来本地访问您的在线数据库:
var mongoose = require( 'mongoose' );
var dbURI = 'mongodb://localhost/Loc8r';
if (process.env.NODE_ENV === 'production') {
dbURI= process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);
And starting your app with "NODE_ENV=production nodemon bin/www"
并使用“NODE_ENV =生产nodemon bin / www”启动您的应用程序
#1
8
Set the NODE_ENV environment variable to "development" on your local environment, and set it to "production" on Heroku. https://devcenter.heroku.com/articles/nodejs#setting-node-env
在本地环境中将NODE_ENV环境变量设置为“development”,并在Heroku上将其设置为“production”。 https://devcenter.heroku.com/articles/nodejs#setting-node-env
#2
0
You can also access your online database locally by starting your app by adding the following:
您还可以通过添加以下内容启动应用程序来本地访问您的在线数据库:
var mongoose = require( 'mongoose' );
var dbURI = 'mongodb://localhost/Loc8r';
if (process.env.NODE_ENV === 'production') {
dbURI= process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);
And starting your app with "NODE_ENV=production nodemon bin/www"
并使用“NODE_ENV =生产nodemon bin / www”启动您的应用程序