Is there a programmatic way to know a node.js app is running in Heroku?

梦想与她 提交于 2021-02-07 12:30:15

问题


Is there some variable or function I can call to know if a node.js application is running inside Heroku? Something like:

if (process.heroku)
  console.log("I'm in Heroku!");

回答1:


You can do this without setting custom environment variables. You can do it like this:

if (process.env._ && process.env._.indexOf("heroku"))
   console.log("I'm in Heroku!");

This is possible because on a Heroku dyno the _ environment variable is set to /app/.heroku/node/bin/node.




回答2:


You use for that usual environment variables. Just set some variable on your heroku instance and check this:

process.env.HEROKU

On the heroku cli you would do: heroku config:set HEROKU=true

You can also set it on the web interface, see heroku docs for more: https://devcenter.heroku.com/articles/config-vars




回答3:


On this specific case I've used if test -d /app; then npm run build; fi which can be used also inside npm-scripts.



来源:https://stackoverflow.com/questions/28472113/is-there-a-programmatic-way-to-know-a-node-js-app-is-running-in-heroku

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!