Changing a value due to .env value

trying to change a value by .env value
using this code

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

trying to change the message due to process.env.MESSAGE_STYLE value
but it is not working

solution: https://free-code-camp-node-js.herokuapp.com

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

Challenge: Use the .env File

Link to the challenge:

what’s your whole code? what’s inside your .env file?

what I see going to the /json endpoint is Cannot read property 'VAR_NAME' of undefined

MESSAGE_STYLE=uppercase is inside my .env

there is VAR_NAME written somewhere?

I am not sure if env values should be changed. They are meant for keeping stuff secret.

Edit: sorry I did not know that this was part of a challenge!

var express = require('express');
const { nextTick } = require('process');
var app = express();

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

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

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

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

module.exports = app;

this is the whole code and the.env is just that line

have you dpeloyed it? I don’t see VAR_NAME anywhere

why do i need VAR_NAME ?
the task says :
Create a .env file in the root of your project directory, and store the variable MESSAGE_STYLE=uppercase in it.
so i did it
process.env.MESSAGE_STYLE
and i tested it

Hello there,

Your app does not appear to be running. That is, when I go to your link with /json, the page never loads.

This could indicate there is something wrong which should be available in the Heroku logs.

Hope this helps

i just noticed that when i run server and go to heroku link /json it didnt do anything (no caps)
but when i go to localhost:3000/json it changed to uppercase !!
checked to log and nothing appeared to be wrong

Have you added the environment variable MESSAGE_STYLE to the Heroku SECRETS section? Heroku does not read off of .env files.

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