Use the .env File: I don't understand somethings?

Use the .env File
Here is my solution:
The key.env file:

# Environment Config

# store your secrets and config variables in here
# only invited collaborators will be able to see your .env values

# reference these in your code with process.env.SECRET

SECRET=
MADE_WITH=
MESSAGE_STYLE=uppercase;

# note: .env is a shell file so there can't be spaces around =

myApp.js:

"use strict";

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

app.use(express.static(__dirname + "/public"));
const upper = process.env.MESSAGE_STYLE === "uppercase";
let message = "Hello json";
if (upper) {
  message = message.toUpperCase();
}
app.get("/json", (request, response) => response.json({
  message: message
}));


//---------- DO NOT EDIT BELOW THIS LINE --------------------

 module.exports = app;

When I run the url/json I get “HELLO JSON”. This is the expected output.
If I change the MESSAGE_STYLE variable I get: “Hello json” as I expected.
But the above code fails on the FCC test… Maybe I don’t understand any serious things?

Thanks for help…

I tried to change the JSON message maybe the JSON format is invalid but this also fails on the test while I get the expected output…

upper 
  ? app.get("/json", (request, response) => response.json({
  message: "HELLO JSON"
}))
  : app.get("/json", (request, response) => response.json({
  message: "Hello json"
}))
;

I think it all has to happen inside one app.get() and you can’t really do any setup outside it (well technically you can put the message string into a variable, but you can’t change it outside the app.get() I think).

Try changing the last code you posted with the ternary to just be one app.get() and replace the upper variable with process.env.MESSAGE_STYLE === "uppercase".


BTW, a quicker way to test if you are passing then having to submit the page every time is to use your glitch URL + /_api/use-env-vars it will serve JSON that looks like this.

{
  "passed": true
}

Or

{
  "passed": false
}

Example URL:
https://env-tests.glitch.me/_api/use-env-vars

1 Like

Thanks, I changed the code and now passed the test…

app.get("/json", (request, response) => {
  let message = "Hello json";
  process.env.MESSAGE_STYLE === "uppercase" ? message = message.toUpperCase() : "";
  response.json({message: message});
});

If you have time can you take a look also to this code please. This is also not passed the test but works fine… I see the html file.
I changed the first solution with variable because later we also used absolute path…
My code now the following:

"use strict";

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

// --> 7)  Mount the Logger middleware here
app.use((request, response, next) => {
  console.log(`${request.method} ${request.path} - ${request.ip}`);
  next();
});

// --> 11)  Mount the body-parser middleware  here


/** 1) Meet the node console. */
console.log("Hello World");
console.log(process.env.PORT);


/** 2) A first working Express Server */
// app.get("/", (request, response) => response.send("Hello Express"));

/** 3) Serve an HTML file */
const absolutePath = __dirname;
app.get("/", (request, response) => response.sendFile(absolutePath + "/views/index.html"));

/** 4) Serve static assets  */
app.use("/", express.static(absolutePath + "/public"));

/** 5) serve JSON on a specific route */

/** 6) Use the .env file to configure the app */
app.get("/json", (request, response) => {
  let message = "Hello json";
  process.env.MESSAGE_STYLE === "uppercase" ? message = message.toUpperCase() : "";
  response.json({message: message});
});

/** 7) Root-level Middleware - A logger */
//  place it before all the routes !


/** 8) Chaining middleware. A Time server */
app.get("/now", (request, response, next) => {
  request.time = new Date().toString();
  next();
}, (request, response) => {
  response.json({
    time: request.time
  });
});

/** 9)  Get input from client - Route parameters */


/** 10) Get input from client - Query parameters */
// /name?first=<firstname>&last=<lastname>

  
/** 11) Get ready for POST Requests - the `body-parser` */
// place it before all the routes !


/** 12) Get data form POST  */



// This would be part of the basic setup of an Express app
// but to allow FCC to run tests, the server is already active
/** app.listen(process.env.PORT || 3000 ); */

//---------- DO NOT EDIT BELOW THIS LINE --------------------

 module.exports = app;