Basic Node and Express - Use the .env File for d0c5i5

Tell us what’s happening:
the code appears to work and changing the environment variable adjusted the output at /json to say {“message”:“HELLO JSON”} not {“message”:“hello json”}, but the site wont properly score my work (has had no issues up until now).

Your code so far
var express = require(‘express’);
var app = express();
console.log(“Hello World " + process.env.MESSAGE_STYLE);
if (process.env.MESSAGE_STYLE === “uppercase”) {
var message = {
“message”: “Hello json”.toUpperCase()
}
} else {
var message = {
“message”: “Hello json”
}
}
// res.json(message);
;
app.get(”/testing2", function(req, res) {
res.send(“Hello Express”);
});
app.get("/jonathan", function(req, res) {
res.send(“Hello Jonathan!!@#@#”);
});
app.get("/testing", function(req, res) {
res.send(__dirname );
});
app.get("/", function(req, res) {
res.sendFile(__dirname + “/views/index.html”);
});
app.get("/json", function(req, res) {
res.json(message);
}
)
app.use(express.static(__dirname+"/public"));

Your browser information:

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

Challenge: Use the .env File

I created the .env file in Files and added MESSAGE_STYLE=uppercase
changing it to anything else has the desired effect, it just doesn’t score me as having it right for some reason.

Link to the challenge:

Put your if conditional inside get(/json) callback function.
And why you initialize variable inside your if condition?

padunk: i tried to move the if statement inside the callback function, but it kept complaining and not starting.

The long and short of it is I believe I got it working, such that if I adjust the .env file contents it DOES capitalize the correct portion.

I’m not sure how the test itself is testing modifications to the .env file, does it somehow access and adjust the contents of the .env file?

Can I see your code?
Because you create the variable inside an if block.
So if you just copy paste it. res.json(message) will return undefined.

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