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:
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: