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:

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

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