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);
});
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
const mySecret = process.env['process.env.MESSAGE_STYLE']
The above code assigns the value of an environment variable named process.env.MESSAGE_STYLE
ito mySecret
instead of value of the correct environment variable named MESSAGE_STYLE
to mySecret
.
Also, your return value when the MESSAGE_STYLE
environment variable is not equal to uppercase
is not correct. Look carefully at the case of each letter specified in the instructions.
2 Likes
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);
});
rafeequeoz7:
const dts = 'Hello Json'
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
You still are not returning the correct message value when the MESSAGE_STYLE
environment variable is not uppercase
. The instructions tell you exactly what to return.
2 Likes
if (mySecret === "uppercase") {
var msg = dts.toUpperCase()
} else {
var msg = dts;
}
else part isn't correct?
It has nothing to do with the else
code. It is what you are initializing dts
to.
could you please clarify ?