Basic Node and Express - Use the .env File

Tell us what’s happening:
Describe your issue in detail here.
As my code works fine but i was just unable to pass the test of freecodecamp.

link : https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/use-the--env-file

let express = require('express');
let app = express();
const absolutePath = __dirname + "/views/index.html"
const MESSAGE_STYLE = process.env.MESSAGE_STYLE


if ( process.env.MESSAGE_STYLE === "uppercase") {
  response = "Hello Json".toUpperCase();
} else {
  response = "Hello Json"
}
//app.use(express.static(__dirname + "/public"))
app.use('/public', express.static(__dirname + "/public"))

app.get('/', function(req, res) {
  res.sendFile(absolutePath);
})

app.get('/json', function(req, res) {
  res.json({"message":response})
})

module.exports = app;

Your project link(s)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

well in the else statement Hello Json shoulde be Hello json.are you using replit or doing locally?

replit
But thanks for your help

You can’t have the if/else outside the route handler as the environment variable needs to be re-evaluated.

Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE

Also, please link to your Replit.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.