I am having issue with the last 10 lines of code where I am working with a .env. I have already added my .env file with the MESSAGE_STYLE=uppercase.
However, when starting the local server with npm start, i get the lower case version of my "hello json" message, not the upper case version.
It also says my secret.env file in root is there and says A now, isntead of U, as undefined, idk what I did differently, however, I am still getting my output as :
{
"message": "Hello json"
}
not what i am expecting to be:
{
"message": "Hello json"
}
Input:
let express = require('express');
let app = express();
require('dotenv').config();
app.use("/public", express.static(__dirname + "/public"))
app.get("/", (req, res) => {
res.sendFile (__dirname + "/views/index.html");
})
app.get ("/json", (req, res) => {
if ( process.env["MESSAGE_STYLE"] == "uppercase"){
res.json({"mesage": "HELLO JSON"});
} else {
res.json({"message": "Hello json"});
}
})
module.exports = app;
2
Answers
Can you change the order of the import?
So instead of having
You have to do
If it’s not helping you can also use the debug mode for
dotenv
Or you can also debug using:
According to the dotenv documentation, the environment variables are loaded from the
.env
file by default. If you want to load them from a customsecret.env
file, you need to configure it: