Basic Node and Express - Use the .env File : Issue with reading built-in SECRETS environment variables in Node.js / Express.js application on Replit

Hello everyone,

I’m working on the FreeCodeCamp challenge “Basic Node and Express - Use the .env File”. The challenge is to modify the response of a route based on an environment variable.

As I’m hosting my project on Replit, I’m using the built-in SECRETS environment variable feature instead of a .env file. I’ve set up a SECRETS variable MESSAGE_STYLE to be "uppercase". The goal is to respond with an uppercase message if MESSAGE_STYLE is set to "uppercase".

Here’s the code I’ve written:

javascript

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

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

module.exports = app;

The problem I’m facing is that even though I’ve set MESSAGE_STYLE to "uppercase", I’m still seeing the lowercase message when I visit the /json endpoint. I can’t figure out why this is happening.

You can find my Replit project here: boilerplate-express - Replit

And here is the link to the challenge: https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/use-the--env-file

I’ve been trying to solve this for a while, but can’t figure out what’s wrong. I would really appreciate any help or pointers on how to debug this.

Thanks!

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

Link to the challenge:

It should be crashing for you with SyntaxError: Unexpected token ')'

This is the code you have on Replit.

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

Your brackets do not match up.

If I fix that, your code passes with my own fork of your project. If it doesn’t pass for you, you may have added quotes to the key or value in the SECRETS which they should not have. It should just be the values, unquoted.

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