Basic Node and Express - Use the .env File

I keep getting this error:

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

However I don’t find any issue in my code;

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

const path = require('path');
const absolutePath = path.join(__dirname, 'views', 'index.html');

app.get('/', (req, res) => {
  console.log('Received request for root route.');
  res.sendFile(absolutePath);
});

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

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

const listener = app.listen(process.env.PORT || 4000, () => {
  console.log(`Your app is listening on port ${listener.address().port}`);
});

And the .env file is located where it should be, in the root directory of boilerplate-express

I don’t understand how to fix this, your help would be appreciated

Your browser information:

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

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

Link to the challenge:

Can you please post a link to your REPL?

I don’t have repl it’s all local

Can you post your ENV file please?

Hi!,
I found a question that apparently is the same in the freeCodeCamp forum:

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