'Static files not reading the .env file in a node js serve

I have a react app that I was able to build into static files which I then added to my node js app. after installing dotenv and adding require('dotenv').config() to server.js in the node server application. The following piece of code I had in the react application does not seem to be working now although the static files are being rendered in the browser just fine.

let node_server_url;
//TODO
// set the NODE_ENV on the .env file.
// create the .env file on the root of the project and add a NODE_ENV on the file
// e.g NODE_ENV=development or NODE_ENV=production

let urlConfig = (process.env.NODE_ENV === "production") ? {
    urlBackend: `https://mybluebookloanuat.bayportbotswana.com`,
} : {
    // urlBackend: `http://10.16.32.22:5000`,
    urlBackend: `http://localhost:5003`,
}

// eslint-disable-next-line
export default node_server_url = urlConfig.urlBackend;

server.js:

require('dotenv').config()
...
const app = express();

// for parsing request body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// cross origin access to allow backend to communicate with frontend
app.use(cors());

const db = require("./app/models/index");
// to drop existing database and resync the database
db.sequelize.sync({force: false})

require("./app/routes/loan_application.routes")(app);


app.use(express.static(path.resolve(__dirname, 'assets')))

app.get('*', (req, res) => {
 res.sendFile(path.resolve(__dirname, 'assets', 'index.html'));
});

I want to be able to just set up in the .env file the address for development and for production in the node server app. Can someone help me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source