我一直在尝试使用mongolab作为我的mongodb在heroku上部署我的风帆应用程序。
我使用了以下连接,一个用于开发,一个用于生产:
local_mongo : {
adapter : 'sails-mongo',
host : 'localhost',
port : 27017,
database : "something"
},
heroku_mongolab : {
adapter : "sails-mongo",
url : "mongodb://*****:******!@ds063160.mongolab.com:63160/something"
}
我还设置了我的local.js文件以使用开发环境并在gitignore上输出该文件。
最后在我的development.js上我做了以下
module.exports = {
models: {
connection: 'local_mongo'
}
};
在我的production.js
上module.exports = {
/***************************************************************************
* Set the default database connection for models in the production *
* environment (see config/connections.js and config/models.js ) *
***************************************************************************/
models: {
connection: 'heroku_mongolab'
}
/***************************************************************************
* Set the port in the production environment to 80 *
***************************************************************************/
//port: 80
/***************************************************************************
* Set the log level in production environment to "silent" *
***************************************************************************/
//log: {
// level: "silent"
//}
};
在我的package.json上,我将启动脚本更改为节点app.js --prod。在本地运行时,一切都按预期工作,即使我运行npm start,但是当我在heroku上部署并查看日志时,我看到它尝试连接到localhost mongodb。
这是一个错误还是我做错了什么?
答案 0 :(得分:1)
您必须使用Procfile。创建一个名为" Procfile"的文件。在应用程序的根目录中,显式声明应该执行哪个命令来启动应用程序。在您的情况下,要定义生产环境,请添加以下行:
web: NODE_ENV=production node app.js
答案 1 :(得分:0)
如果您希望Heroku在生产环境中运行您的应用程序,您必须按如下方式设置环境变量:
heroku config:set NODE_ENV=production