Getting environment variables from nodejs app cpanel - node.js

So I'm using cPanel with Setup Node.js App plugin for an node/express app. (don't asky why cPanel)
Everything is working as expected in development, except for environment variables in production, I set up them manually from the cPanel interface, I restarted/stopped the app, logging the process.env on the server and I don't see the env variables there (not to say when trying to call them they are undefined).
When doing res.json(JSON.stringify(process.env)); i get a bunch of variables except for the one I manually wrote in cPanel variables interface.
It is important for me to store these variables as a secret key because they are API credentials.
Does anyone know what I might have misconfigured or had this problem?

Related

Cloud foundry VCAP_SERVICES in VueJS Appliction

I am writing VueJS Web Application and deploying into Pivotal Cloud Foundry.
I want to read config from user provided VCAP_SERVICES.
I tried two approach to do that.
using cfenv library : But when i do that i am getting Uncaught TypeError: Arguments to path.join must be strings while var cfenv = require('cfenv')
Then i tried manual approach to do that let vcap_services = JSON.parse(process.env.VCAP_SERVICES), in this one i am getting VCAP_SERVICES undefined.
Can anyone please help or suggest something.
You cannot look at VCAP_SERVICES in a client's browser, which is where your Vue.js app is running. This is because VCAP_SERVICES is an environment variable set on the server-side where your application is being hosted.
If you want it available on the client-side, you'd need to expose it in some way. Either encode it in the files being served up to the client's browser or make an API call and request it from your server.
That said, often the information in VCAP_SERVICES is sensitive (contains passwords and secrets), so you'd want to be extremely careful about what you send down to the client/browser. Anything you send down to the client/browser will be visible to the client/browser and its human user. In short, someone using your app through a browser could also see that info, like through Browser Dev Tools.

Heroku: Could not load the default credentials (Dialogflow API)

I am trying to build a chatbot using Node.JS, Dialogflow & Socket.Io and it works fine on my local machine. But when I want to access my agent from my Heroku app I get this error: Could not load the default credentials.
I have defined the environment variables in the Config Vars on Heroku, but it doesn't seems to work that way. Anyone had this problem before?
Thanks,
Matthieu

Environment Variables in Angular2

So I have an angular 2/4 app, with a node server.js to serve it. I want to access an environment variable (backend endpoint is localhost for dev and another endpoint for prod). If it matters, this app (as well as the backend app) are meant to be deployed to Heroku.
I have tried finding a solution to this, but everything I found seems to lead to using multiple environment.ts files (1 for each configuration), and then swapping between them based on the ng build flag --env. But I am apprehensive about this solution as it seems to mix configuration and source code. I also would like to avoid (if possible) being required to re-build/re-deploy whenever an endpoint/config changes.
In Spring Boot, I'm used to using config like ${PORT:8080} in the properties files. Though I've also created multiple .properties files in the past so I understand that sometimes it's just simpler/cleaner to do so.
In node, I'm used to doing env variables like process.env.PORT || 8080. Since my angular app is served by a node server.js, I would think I should be able to do something similar. But when I try using process.env in my environments/environment.ts file, I get the error Cannot find name 'process'.
So my question is essentially, if I am required to create multiple environment property files or is there an alternative.
Thanks
You basically have two choices, regardless of whether your config is stored in environment variables or in a config file on the server. Your clientside code needs it, so you either need for serve it as part of your assets bundle, or you have to fetch it as a separate request. That separate request could be an Ajax call or just a js file that gets served separately from the rest of the bundle.
I think ideally, the only difference between the production app and the nonproduction app on the client should be the data, the bundle density (in dev you don't want your stuff minified for example) and what url it's served from. Everything else should be discoverable from the source url, imo

Node not able to read environment variables in AWS Beanstalk

I can't go into details unfortunately, but I'll try to be as thorough as possible. My company is using AWS Beanstalk to deploy one of our node services. We have an environment property through the AWS configuration dashborad, the key ENV_NAME pointing to the value in this case one of our domains.
According to the documentation, and another resource I found once you plug your variables in you should be able to access it through process.env.ENV_NAME. However, nothing is coming out. The names are correct, and even process.env is logging out an empty Object.
The documentation seems straight forward enough, and the other guide as well. Is anyone aware of any extra steps between setting the key value pair in the dashboard, and console logging out the value once the application is running in the browser?
Turns out I'm an idiot. We were referencing the environment variable in the JavaScript that was being sent to the client. So, we were never looking for the env variable until after it was off the server. We've added a new route to fetch this in response.

Retrieve local and heroku application hostname outside a request

I have to configure one of the module in my application and give it a specific URL, based on the hostname. Something like http://localhost locally and http://myapp.herokuapp.com. I know I can just hardcode the value depending on NODE_ENV, or even store it as an environment value and use process.env.CUSTOMVAR_HOSTNAME. But somehow it feels wrong, since myapp is already configured in the Heroku admin panel. And I can't use request.headers.host since the module configuration happens at the start of the application, before any request can be handled.
Is there a way to retrieve the Heroku application name from the code ? Or from an environment variable ? I want my code to stay the same whether I'm executing code locally or on Heroku (or any other place actually).
I didn't find any way, so I created a local variable hostname based on NODE_ENV (development or production) with the proper hostname value.

Resources