Use the .env File please help

I took the bath “back-end-development-and-apis”
and while I am trying to use solve the exercises I faced this proplem I searched every where but I still receive this error:
// running tests The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE // tests completed

let express = require(‘express’);
let app = express();

if(process.env.MESSAGE_STYLE === ‘uppercase’){
let response = “Hello json”.toUpperCase();
app.get("/json", function(req, res){
res.json({“message”: response});

})
}else{
app.get(’/json’, function(req, res){
res.json({“message”: “Hello json”});

})
}
app.use("/public", express.static(__dirname + “/public”));

app.get(’/’, function(req, res){
res.sendfile(“views/index.html”)

It is best to post actual code instead of a picture.

let express = require('express');
let app = express();


if(process.env.MESSAGE_STYLE === 'uppercase'){
  let  response = "Hello json".toUpperCase();
  app.get("/json", function(req, res){
  res.json({"message":  response});
  
})
}else{
  app.get('/json', function(req, res){
  res.json({"message": "Hello json"});
  
})
}
app.use("/public", express.static(__dirname + "/public"));


  app.get('/', function(req, res){
  res.sendfile("views/index.html")
let express = require('express');
let app = express();


if(process.env.MESSAGE_STYLE === 'uppercase'){
  let  response = "Hello json".toUpperCase();
  app.get("/json", function(req, res){
  res.json({"message":  response});
  
})
}else{
  app.get('/json', function(req, res){
  res.json({"message": "Hello json"});
  
})
}
app.use("/public", express.static(__dirname + "/public"));


  app.get('/', function(req, res){
  res.sendfile("views/index.html")
  
})

 module.exports = app;

The if statement needs to be inside the route handler.

2 Likes

You are determining when the app starts what the response will be. This means that your app can’t respond to changes in the environment variable. Your logic for changing the response based upon the environment variable needs to be inside of the GET.

:sweat_smile: thank you really , that’s was helpful.

1 Like

thanks man, that 's was helpful