Basic Node and Express - Use the .env File

Tell us what’s happening:
I’m kinda certain that I’ve done all I’m suppose to do but my /json endpoint is not responding , it did once and did what the test asked but did not pass the test then suddenly the /json endpoint stopped responding, just kept loading .

Your project link(s)
https://replit.com/@BobbyEnomate/boilerplate-express

solution: https://replit.com/@bobbyenomate/boilerplate-express

Your browser information:

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

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

let express = require('express');
let app = express();
const mySecret = process.env['MESSAGE_STYLE'];
const note = {
      "message": "Hello json"
    }

app.get("/", function (req, res) {
  console.log("Hrllo World")
    res.sendFile(__dirname + "/views/index.html");    
});
app.use("/public", express.static(__dirname + "/public"));

app.get("/json", (req, res) => {
  if (mySecret === "uppercase") {
    note.message = note.message.toUpperCase()
  } else {
     res.json(note);
  }
});

There’s a couple of problems I think.

First is the location of the line of code that reads the env variable is not inside the route handler. If you look at the instructions given again they said:

Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.

Then the actual handling of the if statement when mySecret is uppercase
You set the value of the message in the note object but then nothing happens after that.
(there’s some code missing to actually return the result)

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