Search code examples
node.jsexpressherokustaging

Can I have part of code ignored in Heroku staging


I want to test the behavior of my app by pushing it to the staging app on Heroku. There's a part that saves information to s3 that I want the app to execute when in production, but to skip over while testing it in staging. Is there a way to do this? I've been manually commenting it in and out but it'd be nice if it could automatically do it. I'm using node and express if that helps.


Solution

  • You can feature base your code. Just add an environment variable and check the value of that variable on your code. If the value is true execute the code, if not, then don't. We do those things all the time on different environments when we release new features to reduce the possible impact.

    1. Modify your .env locally to have this new variable.
    2. Add your variable to your app.json (best practice). This will force application to have this variable before launching, hence, nobody would forget to add it. { "name": "my app", "description": "My super app", "scripts": {}, "env": { "MY_ENV_VAR": { "required": true } }

    3.Check the value of the variable on your code:

    if (process.env.MY_ENV_VAR === 'true') { //keep in mind these will be strings //do your logic }

    1. Ensure the env var exist in your heroku dashboard application settings.

    Regards