Basic Node and Express - Use the .env File

Tell us what’s happening:
Hello, my solution works locally on my computer and returns the right JSON value. I converted the solution to the replit link below but the grader is not passing me.

Your project link(s)

solution: boilerplate-express - Node.js Repl - Replit

Your browser information:

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

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

Link to the challenge:

You are not following this instruction as it pertains to WHERE you access the environment variable.

Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE and transform the response object’s message to uppercase if the variable equals uppercase .

Thanks for the response. The solution works on my computer but I am having issue with the Replit version.

I already created the variable from the secrets tab.

let express = require('express');
let app = express();
const mySecret = process.env['MESSAGE_STYLE'];

app.get('/', (req, res) => {
  // res.send(mySecret);
    res.sendFile(__dirname + '/views/index.html');
});


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

app.get('/json', (req, res) => {
  res.json({
    message: (mySecret  === 'uppercase' ? 'HELLO JSON' : 'hello json'),
  });
});

module.exports = app;

Opening the URL below with the browser returns the required JSON, so I don’t know why the grader is not passing me.

https://boilerplate-express.adebayoade.repl.co/json

image

This is in the global space, only checked once when the app is started, so your code cannot respond to changes in the environment variable.

In other words, you are not respecting the instructions on where in your code you need to check the environment variable.

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

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

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

app.get('/json', (req, res) => {
  let resultMessage = 'Hello json';
  if (process.env.MESSAGE_STYLE === 'uppercase') {
    resultMessage = resultMessage.toUpperCase();
  }

  res.json({ message: resultMessage });
});

module.exports = app;

How about this?

Can you please check why the grader is not passing me?

image