Use the .env File, cannot go ahead

Hi, i’m studying this lesson, here is my code in myApp.js:

const mySecret = process.env['MESSAGE_STYLE']
var express = require('express');
var app = express();

app.get("/", function(req, res) {

  res.sendFile(__dirname + '/views/index.html');
});
app.get("/json", (req, res) => {
  res.json({
    message: "Hello json"
   
  });
});


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



module.exports = app;

i ask you if until now it s right, because after i don’t know how to make the secret variable works for get(/json). Hope for a hint!

I’ve just written this:

const mySecret = process.env['MESSAGE_STYLE']
var express = require('express');
var app = express();

app.get("/", function(req, res) {

  res.sendFile(__dirname + '/views/index.html');
});
app.get("/json", (req, res) => {
  res.json({
    if (process.env.MESSAGE_STYLE === uppercase) {
  return {"message": "HELLO JSON"};
} else {
  return {"message": "Hello json"};
}
   
  });



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



module.exports = app;




but it doesn’t work still

Here’s my replit

Your if statement has good logic, but you need to use the res object

I inserted the object with a couple key/value, that’s it:


var express = require('express');
var app = express();
const mySecret = process.env['MESSAGE_STYLE']
app.get("/", function(req, res) {

  res.sendFile(__dirname + '/views/index.html');
});
app.get("/json", (req, res) => {
  res.json({ if (process.env.mySecret === uppercase) {
    message: "HELLO JSON";
  }else {
       message: "Hello json"}
});

   
      



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



module.exports = app;




but it still doesn’t work

That isn’t a valid way to use an if statement. Code blocks don’t have return values. Thus, you aren’t actually returning anything to use in your res object.

If your secret indicates, then you need to use the res object to return a message in all caps, otherwise you need to use the res object to return the message with only the first letter capitalized.

json() is a method on the res object. You use it like any other method inside an if/else statement. The method takes a JSON object as the argument.

if (someCondition) {
  someObject.someMethod(JSON);
} else {
  someObject.someMethod(JSON);
}
1 Like

It’s more difficoult and hard than i thought, i am working on it thank you both.

I tried to follow your steps, cannot solve it, here 's my new replit, hope you can tell me if i’m close to the right way or not.

The key:value format for your JSON should look like this:

“key”:“value”

In your case you have for example:

message: “HELLO JSON”

Notice what is missing in the key part.

Ok,i’ve fixed it, now my code is:

var express = require('express');
var app = express();
console.log("Hello World")

app.get("/", function(req, res) {
  res.sendFile(__dirname + "/views/index.html");
});
app.use("/public", express.static(__dirname + '/public'));
app.get("/json", (req, res) => {if (process.env['MESSAGE_STYLE'] === uppercase){
  res.json({
    "message": "HELLO JSON"
  })} else {
    res.json({
    "message": "Hello json"
  })
  }
});
const mySecret = process.env['MESSAGE_STYLE']































 module.exports = app;

I’ve tried to change the variable’s name used in the if statment, calling it: process.env['MESSAGE_STYLE'] or process.env.MESSAGE_STYLE or even mySecret but if it doesn’t work the same, hope for another and hopefully last hint!

Your uppercase should be “uppercase” since it is a string that needs to be compare to an environment variable. Sorry, I did not see that earlier.

I’ve solved, thank you everybody who helped me! <3

1 Like