Basic Node and Express - Use the .env File - Code works but test won't pass

I’m working on the “Use the .env File” and I’m running the project in the Codesandbox (tried replit but it’s not working correctly at the moment).

I think the code is OK, I’ll paste it here:

require("dotenv").config();

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

module.exports = app;

console.log("Hello World");
console.log(__dirname);

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
});

app.use("/public", express.static(__dirname + "/public"));

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

The code works, when I change the MESSAGE_STYLE value in the .env file I get the correct behavior:

image

The issue is that when I try to pass the test in freeCodeCamp, I get the following:

// running tests
The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE
// tests completed

And the test won’t pass.

At the same time I get an error in the nodejs console:

https://www.freecodecamp.org
node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 167.235.8.252:443
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:481:9)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '167.235.8.252',
  port: 443
}

My solution: https://f3fpg6-3000.preview.csb.app/json

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

Link to the challenge:

1 Like

Ok, Replit is working again (must have renewed their SSL certificates or something like that, I’m no expert).

Still there is one minor concern that I want to address: the instructions point towards creating a .env file but that is not possible currently in Replit. It’s deprecated and the way to go is to use the builtin Secrets functionality to add key/value pairs of environment variables. Another option could be to change the code so an environment file with a slightly different name is loaded.

Note: If you are using Replit, you cannot create a .env file. Instead, use the built-in SECRETS tab to add the variable.

The instructions tell you this already.

1 Like

Oh my bad then, sorry. I didn’t read everything again when migrating fromcodesandbox to replit. Shame on me :sweat_smile:

No problem, in all fairness that page does have a lot of text and you wouldn’t be the first to have missed the note.

1 Like

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