Basic Node and Express - Use the .env File

Tell us what’s happening:

why am I getting error?

Failed:The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE

Challenge Information:

Basic Node and Express - Use the .env File

my code in myApp.js is as follows:

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

console.log("Hello World")

//Serve an Html file
app.get("/", function(req, res) {
  res.sendFile(__dirname + "/views/index.html");
})

//serve static assests
abspath = __dirname +"/public"
app.use("/public", express.static(abspath))


//serve json
app.get("/json", function(req,res){
    if(process.env.MESSAGE_STYLE=="uppercase"){ 
        res.json({message: "Hello json".toUpperCase()});
    }
 else
    { res.json({message: "Hello json"});  
 
})


 module.exports = app;

why am I getting error?

Failed:The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE

project link

https://freecodecam-boilerplate-xre7fa46ukp.ws-us108.gitpod.io

Your browser information:

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

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

what’s in your .env file?

this is .env file:

MESSAGE_STYLE=uppercase

I had to use

require('dotenv').config()

and it worked. Thank you

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