Basic Node and Express - Use the .env File

Tell us what’s happening:
I got my task correct but it’s showing error

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

boilerplate-express - Replit

solution: https://boilerplate-express.kyawsan834.repl.co/JSON

if (process.env.MESSAGE_STYLE === 'uppercase') {

  app.get('/json', function(req, res) {
    res.json({
      "message": "Hello json".toUpperCase()
    });
  });
} else {
  app.get('/json', function(req, res) {
    res.json({
      "message": "Hello json"
    });
  });
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0

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

I have edited your post to include a direct link to the challenge and also to display your code correctly on the forum.

The challenge instructions:

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.

You shouldn’t be putting your route inside the conditional statement. You should write the route and put the conditional statement inside it.

Also, the link for your solution should be: https://boilerplate-express.kyawsan834.repl.co (i.e. without the /JSON route).

okay sir, I will try it

I change it
It’s not working

let express = require('express');
let app = express();
// require("dotenv").config();

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

console.log("Hello World");

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

  let absolutePath = __dirname + "/views/index.html";

  console.log(absolutePath);
  res.sendFile(absolutePath);

});

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

//   // res.json({
//   //   "message": "Hello json"
//   // });



//   if (process.env.MESSAGE_STYLE === 'uppercase') {

//     res.json({
//       "message": "Hello json".toUpperCase()
//     });
//   } else {
//     res.json({
//       "message": "Hello json"
//     });
//   }




// }); 



app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === "uppercase") {

    res.json({
      "message": "Hello Json".toUpperCase()
    });

  } else {
    res.json({
      "message" : "Hello Json"
    });
  }
});










































module.exports = app;

the output is correct but result is

// running tests The response of the endpoint

/json

should change according to the environment variable

MESSAGE_STYLE

// tests completed // console output [Error: Not Found]

The response of the endpoint sir

/json

should change according to the environment variable

MESSAGE_STYLE

// tests completed

This is not the correct lowercase response. It should be ‘Hello json’.

Okay, from the information provided it seems like you have correctly implemented the logic to conditionally return uppercase or lowercase JSON response based on the MESSAGE_STYLE environment variable.

However, the challenge is still showing an error because it is expecting the actual response to change when the environment variable is changed, not just the code implementation.

Some things to check:

  • Make sure the app is properly reading the environment variable value from the .env file. Print out process.env.MESSAGE_STYLE to verify.

  • When you change the .env file, restart the Node.js process so it picks up the new env variable value.

  • Make a request to the /json endpoint both before and after changing the .env file, and verify the response changes as expected based on uppercase vs lowercase.

  • Check if there are any errors in the Node.js process output when starting/restarting after changing .env

  • Try a simple console.log inside the routes to verify conditionals are executing properly.

The key is that the actual response needs to change for the check to pass, not just the code logic. Let me know if any of these suggestions help troubleshoot further!

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