Use the .env file - Can't complete the challenge?

When I try to submit the challenge on this step in Basic Node and Express, I get the following message:

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

My code does that, but the passoff isn’t accepting it for some reason. Below is the code snippet I use in the glitch app to get it to work.

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

  var response = "Hello json"
  if (process.env.MESSAGE_STYLE == "uppercase") {
    response = response.toUpperCase();
  } 
  res.json({
    message: response
  });
})

What does freecodecamp want me to put as my answer then if it won’t accept this? The logs don’t show anything abnormal. A link to the project I’m using: https://glitch.com/~caring-occipital-gull

Pretty sure it’s a bug.

Make sure you get the latest version of the fCC dependency.

"fcc-express-bground": "https://github.com/freeCodeCamp/fcc-express-bground-pkg.git"

Open your package.json and remove the package. Wait for Glitch to finish removing it (look at the Tools spinning icon and wait) then add it back and wait for the install to finish. You should now be able to complete the challenge.


Edit: You can check that you have the correct dependency by opening up the browser console (F12) and submitting it. If you get this error Error: Unexpected token < in JSON at position 0 then you do not have the latest version of the dependency.

1 Like

Hi everyone… i have like 6-7 days attempting to do this challenge on repl.it and nothing for me is working. Any suggestions on what I should do?

This is the solution that passed the test for me:
The code for myApp.js is below:

var express = require('express');
require('dotenv').config();
var app = express(); 
process.env.MESSAGE_STYLE == "uppercase";
console.log(process.env.MESSAGE_STYLE);

//let app = express();
//console.log(process.env//.VARIABLE_ONE)


//app.use('/pics', express.static(__dirname + '/images'));
//console.log("Hello World");
/** app.get("/", (req,res)=>{
  res.send("Hello Express");

});*/

// app.get("/",function(req,res){
  //res.sendFile(__dirname + "/views/index.html");
//});

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

//serve json on a specific route
var message = {"message": "Hello json"};
//app.get('/json',(request,response)=> {
  //response.json(message);
//});

/**6)use the .env File to configure app*/

app.get("/", function(req, res){
  res.sendFile(__dirname+ '/views/index.html');
app.use(express.static(__dirname + "/public"));
})
//7.use the .env file
app.get("/json", (request, response)=>{
  if(process.env.MESSAGE_STYLE ==uppercase){
     response.json({"message" : "HELLO JSON"})
  }else{
    response.json(message)
  }
});
module.exports = app;
  

Code for the .env file is below:

MESSAGE_STYLE=uppercase;

I managed to pass the test with the above code blocks, so, you can try it out

My url link to the result:

2 Likes