【发布时间】:2015-04-12 20:56:24
【问题描述】:
是否有一些变量或函数我可以调用以了解 node.js 应用程序是否在 Heroku 中运行?比如:
if (process.heroku)
console.log("I'm in Heroku!");
【问题讨论】:
是否有一些变量或函数我可以调用以了解 node.js 应用程序是否在 Heroku 中运行?比如:
if (process.heroku)
console.log("I'm in Heroku!");
【问题讨论】:
您可以在不设置自定义环境变量的情况下执行此操作。你可以这样做:
if (process.env._ && process.env._.indexOf("heroku") !== -1)
console.log("I'm in Heroku!");
这是可能的,因为在 Heroku dyno 上,_ 环境变量设置为 /app/.heroku/node/bin/node。
【讨论】:
indexOf 将返回 0 作为有效结果,其评估结果为 false。它返回 -1 表示没有结果会评估为真。这些都不正确。要修复,请更改为 (process.env._.indexOf("heroku") !== -1)
您使用通常的环境变量。 只需在您的 heroku 实例上设置一些变量并检查:
process.env.HEROKU
在 heroku cli 上,您可以:
heroku config:set HEROKU=true
您也可以在 Web 界面上进行设置,请参阅 heroku 文档了解更多信息: https://devcenter.heroku.com/articles/config-vars
【讨论】:
在这个特定情况下,我使用了if test -d /app; then npm run build; fi,它也可以在 npm-scripts 中使用。
【讨论】: