0

I'm not sure if it is possible to do something like this.

I have two Reactjs project p1 and p2. For the server, I'm using Loopback3 and MongoDB. Both projects will connect to one server.

What I want is to create multiple host database in one server so if I login to p1 project, it will use p1 database. If I login to p2 project, it will use p2 database.

Each p1 and p2 will have it own environment for development and production

I have the NODE_ENV to check for p1 and I can separate the development and production. I use the same method to create datesource.p2.json and server.p2.js, but I cannot change the NODE_ENV to use p2 database.

Here is the script in package.json

  "scripts": {
    "heroku-prebuild": "npm install",
    "start-P2": "node ./server/server.p2.js",
    "start": "node ./server/server.p1.js",
    "posttest": "npm run lint && nsp check",
    "devserver": "nodemon ./server/server.js"
  },
Jason
  • 21
  • 2
  • #1 Do you choose that way of working to save money? #2 It is for an enterprise/company , learning or just for fun? #3 If you could have at least 2 servers (dev/prod), Are you open to ear a minimal version of architectures used in companies (devops, docker, git, webhook, etc) ? – JRichardsz Mar 17 '23 at 04:47
  • It's to save development time. If we used two server and we updated an API, we would have to push the update code to two different server. The purpose is one code base, but the API can be shared to multiple clients and each client will have it own database. – Jason Mar 20 '23 at 22:53
  • Using docker and webhooks, only one push is required. After that with docker one build is performed and then it is deployed to any server like test, pre-prod, prod, etc. One server is just for pocs – JRichardsz Mar 21 '23 at 00:30
  • Thanks. I think I got what you mean. It is new to me. Maybe I miss understand and make thing more complicate. My current project is hosting on Heroku and it has dev/prod. Now I want to add 2 more database (dev/prod) for better management. Does it mean I will have to create 2 new host (dev/prod) on Heroku? Also, add host, port, username, password and other properties to setup connection properly. – Jason Mar 21 '23 at 20:23
  • Nice requirement. So your question should be: How to handle multiples environments (dev/prod) in one nodejs app? Please update your question with your comment details, to help you – JRichardsz Mar 21 '23 at 23:01

1 Answers1

0

As long as you are on a unix-based system, you can do something like this in your script:

"scripts": {
    ...
    "start-P2": "env_name=p2 node ./server/server.p2.js",
    "start": "env_name=p1 node ./server/server.p1.js",
    ...
  },

where env_name is the name of the environment variable that you will use to detect which environment to connect to. Then in your code just check whether that variable equals p1 or p2 (or something else).

chill389cc
  • 365
  • 1
  • 10
  • It looks like my server is working now. I can set the NODE_ENV and the front-end will connect to p1 or p2 database. However, if I test my server on local, I will have to manually change the env in NODE_ENV before I can do more test on front-end. – Jason Mar 16 '23 at 20:33