Enviroment variables NodeJS + Express

Tell us what’s happening:
I tried to send json file with “uppercase” if environment variable process.env.MESSAGE_STYLE = uppercase. But I get an error. I tried several times but I couldn’t get it to work :frowning: Some help please?
If you’ll give me a link to more examples like this I would appreciate it even more :slight_smile:

Your code so far
var express = require(‘express’);
var app = express();
const port = process.env.PORT;
process.env.MESSAGE_STYLE=“uppercase”

/** 3) Serve an HTML file */

app.get(’/’,(req, res) => {
res.sendFile(__dirname + ‘/views/index.html’)
})
/** 4) Serve static assets /
app.use(express.static(__dirname + ‘/public’))
app.use(’/assets’,express.static(__dirname + ‘/public’))
/
* 5) serve JSON on a specific route */
/*app.get("/json", (req, res) => {
res.json({
message: “Hello json”
});
});

/** 6) Use the .env file to configure the app */
app.get("/", (req, res) => {
if (process.env.MESSAGE_STYLE === “uppercase”) {
res.json({
message: “Hello JSON”.toUpperCase()
})
} else res.json({
message: “Hello JSON”})
}
);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

Environment variables must be in a separate file named .env, not in the js file.
Also .env values don’t need quotes.

I moved the env variable to the file .env but it didn’t help me :frowning:
I still couldn’t pass my test. I got 0 errors :frowning:
app.get(’/json’, (req, res) => {
if(process.env.MESSAGE_STYLE === ‘uppercase’){
return res.json({
message: ‘Hello json’.toUpperCase()
});
}else{
return res.json({
message: ‘Hello json’})
}
});

show your .env file, or better put the link to repl

1 Like

This is a link to my repl
repl.it/@Fewwy/boilerplate-express

can’t see .env file :thinking:

1 Like

There it is :slight_smile:

Aaaand I solved it…
I forgot to delete “process.env” from the .env file so the only thing I had left was variable name and declaration MESSAGE_STYLE=uppercase
After that my code worked just fine:
app.get(’/json’, (req, res) => {
if(process.env.MESSAGE_STYLE === ‘uppercase’){
return res.json({
message: ‘Hello json’.toUpperCase()
});
}else{
return res.json({
message: ‘Hello json’})
}
});

Thanks for your help!!!

Looks like on repl only owner can see the .env :slight_smile:
Ok, then post the content of .env

1 Like