Basic Node and Express - Use the .env File

Tell us what’s happening:
Describe your issue in detail here.
Please tell me where I am getting wrong and unable to pass the test case. I have set in the secret tab of repel Key as MESSAGE_STYLE while the value as uppercase as .env file but my test is not running.

Your project link(s)

solution: boilerplate-express - Node.js Repl - Replit

Your browser information:

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

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

You need to check the value of the environment variable while your callback function runs. Currently you only check the value when the app starts, but the tests work by changing the environment variable value.

Sorry I cant understand much can you please elaborate more? Like where I need to check in repl ?

You must check the environment variable inside of the callback function for handling the GET.

Please tell what is callback function and how to move my environment variable there?

You perhaps missed a lot of previous content if you don’t know what a callback function is.

In Express, routes takes the following structure: app.METHOD(PATH, HANDLER). METHOD is an http method in lowercase. PATH is a relative path on the server (it can be a string, or even a regular expression). HANDLER is a function that Express calls when the route is matched. Handlers take the form function(req, res) {…}, where req is the request object, and res is the response object. For example, the handler

function(req, res) {
  res.send('Response String');
}

will serve the string ‘Response String’.

The handler is a callback function.

const mySecret = process.env[‘MESSAGE_STYLE’]
var express = require(‘express’);
var app = express();

app.get(“/”, function(req,res){
res.sendFile(__dirname+“/views/index.html”)
});

app.get(“/json”, function(req, res){
if (mySecret === “uppercase”){
res.json({message: “hello json”.toUpperCase()});
}
else
res.json({message: “hello json”} );
});

app.get(“/json”, (req, res) => {
res.json({
message: response
});
});

please check i Have done the same but unable to get the test case passed.

This is not inside of the callback function.

But why it should be done please provide reason

If you check the environment variable outside of the GET handler, then the value is only checked once, when you start the app by clicking run on Replit. But the test changes the value of the environment variable while your app is running, so you need to check the value every time the GET handler (that callback function you set) runs.

Did you mean my env MESSAGE_STYLE is changing automatically while I click run on Replit ?

Sort of. When you submit the link on freeCodeCamp, the test suite sends commands to your Replit that changes the environment variables for each test case.

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