curriculum icon indicating copy to clipboard operation
curriculum copied to clipboard

Correct code on "Basic Node and Express - Use the .env file" does not work

Open nkolatsis opened this issue 6 years ago • 8 comments

Describe your problem and - if possible - how to reproduce it

The following fails:

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

And this works:

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

They are basically the same code. I'm not sure how the testing is done but the first code segment should work fine. It is not a big deal, although I am sure fixing this should save some people a little frustration.

Add a Link to the page with the problem

https://learn.freecodecamp.org/apis-and-microservices/basic-node-and-express/use-the--env-file

nkolatsis avatar Jun 20 '18 00:06 nkolatsis

Another one on implementing a root-level request logger middleware

// --> 7) Mount the Logger middleware here app.use((req, res, next) => logger(req, res, next)); This does not work: /** 7) Root-level Middleware - A logger */ function logger(req, res, next) { console.log(req.method, req.path, "-", req.ip); next(); }

And this does work: /** 7) Root-level Middleware - A logger */ function logger(req, res, next) { console.log("" + req.method + " " + req.path + " - " + req.ip); next(); }

The console output is exactly the same.

nkolatsis avatar Jun 20 '18 01:06 nkolatsis

@nkolatsis

The following fails:

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

The above code is failing because is is not returning the correct response. Notice the code message = message.toUpperCase(). Here message is an empty string.

The other code is passing the test because you have let message = 'Hello json';

I hope you understand where the problem is. If there is any confusion then feel free to ask.

anku255 avatar Jun 28 '18 05:06 anku255

Hi! @Em-Ant

Please check the above comment. Both of the logger functions are outputting the exact same thing but one is passing the test and one is not.

I copied the RegEx you have used here and tested on RegEx101 for both the outputs and it seems to be working fine. So, I am not sure where the problem is.

It would be great if you could take a look.

anku255 avatar Jun 28 '18 05:06 anku255

Hi @nkolatsis and @anku255 , about the .env challenge @anku255 is correct. message.toUpperCase() is an empty string when the test fails. About the other issue, the problem is in this function in the background server package. It only tests the first argument of console.log and expects it to be a string.

We can require in the description that the log has to be a single string, or we could try to change the implementation concatenating the console.log args.toString() and test the result.

What do you think guys ?

(@raisedadead, I remember that you asked me to move this package from my github account to freecodecamp, but I don't remember if I did. If not, can you help me ? Sorry about that)

Em-Ant avatar Jun 30 '18 16:06 Em-Ant

@anku255 @nkolatsis I updated the boilerplate.

It should work for the glitch projects created from now on. For the old ones there is a problem: the npm modules are cached. If you want to do a clean re-install you have to access the linux console of your project (clicking on the relative button on the right of Activity Log, or at the url https://glitch.com/edit/console.html?{your-project}) and delete the files in the folder /tmp/npm-cache. Otherwise you have to wait for the cache to expire to get the new version of the package. I hope it helps.

Em-Ant avatar Jun 30 '18 19:06 Em-Ant

@Em-Ant can you please DM on gitter, I'll walk you through the steps for the transfer

raisedadead avatar Jun 30 '18 19:06 raisedadead

I'm getting no found here. Even if I can show the JSON response on the browser using the [glitch_url]/json. I assume is a testing problem as it works perfectly for me.

https://platinum-windscreen.glitch.me/json

app.get("/json", function(req, res) {
  let message = 'Hello json'
  if (process.env.MESSAGE_STYLE === 'uppercase') {
    return res.status(200).json({"message": message.toUpperCase()})
  }
  return res.status(200).json({"message": message})
})

.env file is

MESSAGE_STYLE="uppercase"

adrianwix avatar Sep 09 '18 17:09 adrianwix

Same with the message

The response of "/json" does not change according to MESSAGE_STYLE

Yolo390 avatar Sep 11 '18 08:09 Yolo390