Use the .end file

Tell us what’s happening:
runs and show results as expected but don`t pass the test. I think it expects a solution without if. can you suggest anything? My key is MESSAGE_STYLE and its value is uppercase.

var express = require(‘express’);
var app = express();
absolutePath = __dirname + “/views/index.html”
assetsAbsolutePath = __dirname + “/public/style.css”

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

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

let json = process.env.MESSAGE_STYLE ? {“message”: “HELLO JSON”} : {“message”: “Hello json”}
res.json(json);
});

app.use("/", function(req, res) {
res.sendFile(assetsAbsolutePath);
});

express.static("/public", )

Your project link(s)

solution: https://replit.com/@cidsantiagosv/boilerplate-expressjson

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

I don’t think that this ternary does what you think that it does.

if process.env.MESSAGE_STYLE is true it shows the first json, if false shows the second. isn`t it?

When would that value ever be a boolean?

When MESSAGE_STYLE have a value it is true, even when I pass process.env.MESSAGE_STYLE == “uppercase” it says it is the wrong answer
app.get("/json", (req, res) => { let message = "Hello json"; (process.env.MESSAGE_STYLE == "uppercase") ? message=message.toUpperCase() : message=message; res.json({"message": message}); });

What does this do?

You seem to be missing some fundamental Javascript knowledge.

I recommend using actual if statements until you are more comfortable with ternaries.

that is literally the ‘get hint’ button response on the exercise, I don`t think they would let a wrong answer.

That’s literally not what’s written there.

You don’t seem to be comfortable converting if statements to ternaries.

I think if you do a typeof check…

console.log(typeof process.env.MESSAGE_STYLE)

…you might see something that would cause an issue?

Compare that to what you get in the browser console.

typeof undefined
// 'undefined'

You can also test it yourself, create a test key and set it to some 'someValue' now log it and then reassign it to undefined

console.log(process.env.test) // someValue
console.log(typeof process.env.test) // string
process.env.test = undefined;
console.log(process.env.test) // undefined
console.log(typeof process.env.test) // string

You are correct in your first assumption that when the environment variable has a string value it is a truthy value.

const env = {
  key: 'value'
}

console.log(env.key ? 'True value' : 'False value'); // True value

env.key = undefined

console.log(env.key ? 'True value' : 'False value'); // False value
1 Like

Just to be super clear.

The test in the background sets process.env.MESSAGE_STYLE to undefined however the environment variable does not contain the primitive values undefined it holds the string 'undefined'. Which is a truthy value.

process.env

Assigning a property on process.env will implicitly convert the value to a string.

Example:

process.env.test = null;
console.log(process.env.test);
// => 'null'
process.env.test = undefined;
console.log(process.env.test);
// => 'undefined'

For it to work I believe the test would have to delete the property and not set it to undefined

1 Like

Thank you lasjorg, when I did a typeof it returns a string, I think it expects a obj. Your answer clarified it to me.

‘it expects an obj’? I’m not sure what you are talking about.

You have written you code in a way where it is hard to see what you are doing

app.get("/json",
  (req, res) => {
    // Set default value of the message
    let message = "Hello json";
    // ternary, but WHY?
    // This isn't how you use a ternary
    (process.env.MESSAGE_STYLE == "uppercase") ? message=message.toUpperCase() : message=message;
    // Respond with message
    res.json({"message": message});
  }
);

A ternary has a return value. Any time you are throwing away a return value, you should be suspicious of what you are doing.

If you think the way I wrote my code is hard to read is because I am learning to program so I dont have the proper skill to write clean code, besides that, I was struggling to solve this challenge. *"Then, in the /json GET route handler you created in the last challenge, transform the response object's message to uppercase if process.env.MESSAGE_STYLE equals uppercase"* Thats why I am thinking I have to transform process.env.MESSAGE_STYLE to a object.

My current solution is:

app.get("/json", (req, res) => {
  
  const message = process.env.MESSAGE_STYLE;
  let json = "Hello json"; (process.env.MESSAGE_STYLE == "uppercase") ? json=json.toUpperCase() : json=json;
  res.json({"message": json});
  console.log(typeof process.env.MESSAGE_STYLE);
});
app.get("/json",
  (req, res) => {
    // THESE NAMES ARE A BIT CONFUSING
    const message = process.env.MESSAGE_STYLE;
    let json = "Hello json";
    // WHY NOT REPLACE THIS WITH A REAL IF?!?
    // YOU SHOULD NOT USE A TERNARY IF YOU
    // ARE IGNORING THE RETURN VALUE
    // GOOD:
    // let myResult = cond ? resultTrue : resultFalse;
    // BAD:
    // cond ? actionTrue : actionFalse;
    // BETTER:
    // if (cond) {
    //   actionTrue;
    // } else {
    //   actionFalse;
    // }
    (process.env.MESSAGE_STYLE == "uppercase") ? json=json.toUpperCase() : json=json;
    res.json({"message": json});
    // WHY WOULD YOU EXPECT AN OBJECT HERE?
    console.log(typeof process.env.MESSAGE_STYLE);
  }
);

What is the current result from the tests for this code?

// running tests The response of the endpoint

/json

should change according to the environment variable

MESSAGE_STYLE

// tests completed

The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE

The last code you posted is strange, as I put in the comments, but it passes for me when I put it in my repl.

What url are you submitting?

my url:
https://boilerplate-express.cidsantiagosv.repl.co/json

Are you submitting that url with /json or without?

With /json, i assume i didn`t test without. It worked right the way, thank you so much.

I`m embarrassed .-.

We’re all learning as we go