Home > Blockchain >  Can I develop in Node.js for local and server at the same time?
Can I develop in Node.js for local and server at the same time?

Time:11-29

So, I guess the language doesn't really matter but the thing is, I have a file called database.js in a folder. Its code must be different depending on whether I am programming in my local environment, or if the code has to be alocated the server (heroku in my case).

For the local environment, I have a postgresql database, which is different from the one I have as an addon in the heroku server. That's why the code is different. The connection to the database is the 'problem'.

What I do now to solve this, is comment out the code for server, test locally, and when I have something usable, I comment out the code for local, comment in the code for server, and deploy in the server.

Here's the code https://github.com/MauroOyhanart/ControlDeGastosIngresos/blob/main/util/database.js

What solutions can I find? I'm pretty flexible. I'm really new to web programming and I'm looking for tools.

CodePudding user response:

You're probably going to want to use some sort of configuration / environment variables. All if not most hosting services should offer a solution, but since you mentioned Heroku I know they support config variables. A bunch of information about them in different languages and how to add them to Heroku can be found here.

Just a quick summary; in NodeJS you are going to want to use process.env to access environment variables. For example (using fictional functions because that isn't the important part here):

db.connect(process.env.DB_HOST, process.env.DB_PORT);
db.login(process.env.DB_USERNAME, process.env.DB_PASSWORD);

Again look at the link above for the actual useful bit with Heroku. This should do the trick for you.

  • Related