Tell us what’s happening:
I am trying to solve the json task where I have to send json response according to an environment variable. I have created an environment variable and imported a dot env file.
Everything seems to be fine but when I press “I have completed this challenge button” it gives error.
// running tests
The response of the endpoint
/json
should change according to the environment variable
MESSAGE_STYLE
// tests completed
The response of the endpoint `/json` should change according to the environment variable `MESSAGE_STYLE`
*********************************************************************************
**Your code so far**
var express = require('express');
var app = express();
require('dotenv').config();
// --> 7) Mount the Logger middleware here
// --> 11) Mount the body-parser middleware here
/** 1) Meet the node console. */
console.log("Hello World");
/** 2) A first working Express Server */
app.get("/a",(req,res)=>{
res.send("Hello Express")
});
/** 3) Serve an HTML file */
absolutePath = __dirname + "/views/index.html";
app.get("/",(req,res)=>{
res.sendFile(absolutePath);
});
/** 4) Serve static assets */
absolutePath = __dirname + "/public"
app.use(express.static(absolutePath));
/** 5) serve JSON on a specific route */
app.get("/json1",(req,res)=>{
res.json({"message":"Hello json"});
});
/** 6) Use the .env file to configure the app */
let response = '' ;
console.log(process.env.MESSAGE_STYLE);
if(process.env.MESSAGE_STYLE==='uppercase'){
// response = "Hello json".toUpperCase();
response = "Hello json".toUpperCase();
}
else{
response = "Hello json";
}
console.log('Bihy',{"style":response})
app.get("/json",(req,res)=>{
res.json({"message": response });
})
/** 7) Root-level Middleware - A logger */
// place it before all the routes !
/** 8) Chaining middleware. A Time server */
/** 9) Get input from client - Route parameters */
/** 10) Get input from client - Query parameters */
// /name?first=<firstname>&last=<lastname>
/** 11) Get ready for POST Requests - the `body-parser` */
// place it before all the routes !
/** 12) Get data form POST */
// This would be part of the basic setup of an Express app
// but to allow FCC to run tests, the server is already active
/** app.listen(process.env.PORT || 3000 ); */
//---------- DO NOT EDIT BELOW THIS LINE --------------------
module.exports = app;
Here is the REPL repository
“https://repl.it/@HasnatAli/boilerplate-express-6#myApp.js”
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0.
Challenge: Use the .env File
Link to the challenge:
