My Solution is correct , but when I submit the solution link it say's isn't correct | Basic Node and Express - Use the .env File

Tell us what’s happening:
Describe your issue in detail here.

Your project link(s)
My Solution is correct , but when I submit the solution link , it says

solution: https://boilerplate-express.rafeequeoz.repl.co/json
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:

Try removing the “/json” from the url you submitted

1 Like

i tried almost optoins like added a # to url , remove ‘/json’…

The test needs to access a live url site so it can test the routes. It looks like it is saying that your /json route is not behaving correctly. How do we see your code to check?

1 Like
let express = require('express');
let app = express();
const mySecret = process.env['process.env.MESSAGE_STYLE']
// app.use(express.static(__dirname + "/public"));
// app.use(express.static(path.join(__dirname , '/public')))
app.use("/public", express.static(__dirname + "/public"));

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
  console.log("Hello World")
})
const  dts = 'Hello Json'
if (mySecret === "uppercase") {
  var msg = dts.toUpperCase()
} else {
  var msg = dts;
}

const dt ={ message:msg }

app.get("/json", (req, res) => {
  res.json(dt);
});

https://replit.com/@RafeequeOz/boilerplate-express#myApp.js

thanks for sharing this. The problem here is that the variable mySecret is being set outside the route handler for the /json route.
Try moving this inside the /json route handler so that your app can pick up on changes to the variable as they’re made by the testing code.

1 Like

Thanks Brother, let me try.

Bro,. I changed the code according to you. But unfortunately it doesn’t work.

can I see your new code pls?

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

// app.use(express.static(__dirname + "/public"));
// app.use(express.static(path.join(__dirname , '/public')))
app.use("/public", express.static(__dirname + "/public"));

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
  console.log("Hello World")
})


app.get("/json", (req, res) => {
  
const mySecret = process.env['process.env.MESSAGE_STYLE']
  const  dts = 'Hello Json'
  if (mySecret === "uppercase") {
  var msg = dts.toUpperCase()
} else {
  var msg = dts;
  }
const dt ={ message:msg }
  res.json(dt);
});

































module.exports = app;

As per Randel’s earlier post, please fix this line.
You need to use process.env to get the MESSAGE_STYLE, not the value of process.env.MESSAGE_STYLE

1 Like

But, when i check the value in console.log its uppercase…

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

// app.use(express.static(__dirname + "/public"));
// app.use(express.static(path.join(__dirname , '/public')))
app.use("/public", express.static(__dirname + "/public"));

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
  console.log("Hello World")
})


app.get("/json", (req, res) => {
  
const mySecret = process.env['process.env.MESSAGE_STYLE']
  console.log(mySecret)
  const  dts = 'Hello Json'
  if (mySecret === "uppercase") {
  var msg = dts.toUpperCase()
} else {
  var msg = dts;
  }
const dt ={ message:msg }
  res.json(dt);
});

This is not quite the correct lowercase message.

Also, your variable names are very cryptic.

Also, I would completely avoid using var.

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

// app.use(express.static(__dirname + "/public"));
// app.use(express.static(path.join(__dirname , '/public')))
app.use("/public", express.static(__dirname + "/public"));

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
  console.log("Hello World")
})


app.get("/json", (req, res) => {

  const mySecret = process.env.MESSAGE_STYLE;
  console.log(mySecret)
  const dts = 'hello json'
  if (mySecret === "uppercase") {
    var msg = dts.toUpperCase()
  } else {
    var msg = dts;
  }
  const dt = { message: msg }
  res.json(dt);
});

































module.exports = app;

Changed to this…but not get the result

 if (mySecret === "uppercase") {
    var msg = dts.toUpperCase()
  } else {
    var msg = dts;
  }

else part isn't correct?

could you please clarify ?

I think you maybe did not read or did not understand any of what I said?

I don’t understand :pensive:

Try to reread the exercise.

What is the objective or goal?