Basic Node and Express - Use the .env File

Facing issue with Node and Express related exercise.
Excercise: Use the .env File
Below is the code present in myApp.js in replit:

let express = require('express');
let app = express();
const path = require('path');

app.listen(3001)

var response;
//console.log(.env)
const mySecret = process.env['MESSAGE_STYLE']
if (mySecret === "uppercase") {
  response = "Hello Json".toUpperCase();
} else {
  response = "Hello json";
}
app.get("/json",(req, res)=>{ res.json({
                              message:response
                            });})

Issue:
Content in the secret file is intact , able to see the excepted result as well.
But the solution is not being accepted and the test cases are failing.

Your project link(s) : boilerplate-express - Replit

solution: boilerplate-express - Replit

Your browser information:

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

Challenge Information:

Basic Node and Express - Use the .env File

At the top of your myApp.js file, add require('dotenv').config() to load the environment variables.
you must read the value of process.env.MESSAGE_STYLE

1 Like

Thank you.

I still find that the issue remains same on replit. Below is my updated code

let express = require('express');
//const process=require('dotenv').config();
require('dotenv').config();
let app = express();
const path = require('path');

app.listen(3001)

var response;

const mySecret = process.env.MESSAGE_STYLE
if (mySecret === "uppercase") {
  response = "Hello Json".toUpperCase();
} else {
  response = "Hello json";
}
app.get("/json",(req, res)=>{ res.json({
                              message:response
                            });})

use the MESSAGE_STYLE environment variable in the route handler and transform the response message accordingly

put all thet in the route handler…

Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.

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