Basic Node and Express - Use the .env File

Tell us what’s happening:

I can’t get this to pass with my solution or anyone else’s in the forums.

app.get(‘/json’, (req, res) => {

var message="hello json"

if ( process.env.MESSAGE_STYLE === "uppercase" ) {
    message = message.toUpperCase();
}
res.json({"message": message});

});

###Your project link(s)

solution: http://localhost:3000

Your browser information:

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

Challenge Information:

Basic Node and Express - Use the .env File

The issue may be this:

The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"} , depending on the MESSAGE_STYLE value.

Your lowercase value for message does not match the requirements.

1 Like

Thank you for pointing that out, but unfortunately that is not the issue. The main issue is the function is not changing the message to the capitalized version when I add the json path to the simple browser. Here is all my code in case the error is somewhere else.

let express = require(‘express’);
let app = express();

console.log(‘Hello World’);

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

app.use(‘/’, (req, res, next) => {
console.log(req.method + ’ ’ + req.path + ’ - ’ + req.ip);
next();
});

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

app.get(‘/json’, (req, res) => {

var message="Hello json";

if ( process.env.MESSAGE_STYLE === "uppercase" ) {
    message = message.toUpperCase();
};

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

});

have you restarted the server after your changes?

1 Like

Yes. Here is my .env file variable: MESSAGE_STYLE=uppercase

This I have experimented adding and moving a “;” around after the variable and can’t get any iteration to work.

absolutely no ; in there

do you have dotenv? or something equivalent?

1 Like

That was it, I misread the bottom paragraph of the lesson.

Thank you SO much haha.