Basic Node and Express - Use the .env File

Tell us what’s happening:
please help me!
I’ve been stuck on this challenge for days and can’t get it to pass. I’ve tried almost every solution available on the forum and the web.

  1. changing dot notation to bracket notation for accessing the variable.
  2. changing the message instead of using the toUpperCase() method.
  3. saving the variable in a const instead of accessing it directly in the if condition.
  4. declaring a response object and changing the message later.
  5. updating the packages already installed by the GitHub repo.

none of the above worked for me.

I’m using app.cyclic.sh as my free host and I’m setting the MESSAGE_STYLE variable inside the variables section. I would put every piece of information I think is necessary here.

I would really appreciate your help, I desperately need the challenge to pass as it’s really influential on my career. thanks a lot!

Your project link(s)

solution:

require('dotenv').config();
var express = require('express');
var app = express();


// console.log('Hello World');

// app.get("/", (req, res) => {
//     res.send("Hello Express");
// });

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

app.get("/",(req,res)=>res.sendFile(__dirname + '/views/index.html'));


app.get("/json", (req,res) => {
    const mySecret = process.env['MESSAGE_STYLE']
    if (mySecret === "uppercase") {
        res.json({
            "message": "HELLO JSON"
        });
    }
    else {
        res.json({
            "message": "Hello json"
        });
    }
    
});



 // 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;

Your browser information:

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

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

Link to the challenge:

Post a repo and the live link as well.


Never used cyclic but I would make sure dotenv is only used in dev (or just comment it out to test) and see if that does anything. Or use env-cmd as suggested in the docs so it only runs in dev mode and not when deployed.


You can always use Replit or Glitch instead if that doesn’t work.

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