Basic Node and Express - Use the .env File

Tell us what’s happening:

hi in my project the systm do not load the envroiment variable project https://3000-freecodecam-boilerplate-3fg5arlmqrv.ws-eu115.gitpod.io/json

###Your project link(s)

solution: https://3000-freecodecam-boilerplate-3fg5arlmqrv.ws-eu115.gitpod.io

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

Basic Node and Express - Use the .env File

can you share the code with us?
Have you created the .env file in the root path?

Also have you added the require dotenv package to the top of the code as the instructions say?

My app

require('dotenv').config()
let express = require('express');
let app = express();

server.js

/******************************************************
 * PLEASE DO NOT EDIT THIS FILE
 * the verification process may break
 * ***************************************************/
 
const bGround = require('fcc-express-bground');
const myApp = require('./myApp');
require('dotenv').config()
const express = require('express');
const app = express();

if (!process.env.DISABLE_XORIGIN) {
  app.use((req, res, next) => {
    const allowedOrigins = ['https://narrow-plane.gomix.me', 'https://www.freecodecamp.com'];
    const origin = req.headers.origin || '*';
    if(!process.env.XORIG_RESTRICT || allowedOrigins.indexOf(origin) > -1){
         console.log(origin);
         res.setHeader('Access-Control-Allow-Origin', origin);
         res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }
    next();
  });
  app.use("/public", express.static(__dirname + "/public"));
  app.get("/", (req, res) => {
    res.sendFile(__dirname + "/views/index.html");
  });
  app.get('/json', (req, res) => {
    let message = "Hello json";
    console.log(process.env.MESSAGE_STYLE)
    // Verifica il valore di MESSAGE_STYLE e trasforma il messaggio
    if (process.env.MESSAGE_STYLE === "uppercase") {
      message = message.toUpperCase();
    }

  // Risposta in formato JSON
  res.json({ "message": message });
});
}

const port = process.env.PORT || 3000;
bGround.setupBackgroundApp(app, myApp, __dirname).listen(port, () => {
  bGround.log(`Node is listening on port ${port}...`);
});

/******************************************************
 * PLEASE DO NOT EDIT THIS FILE
 * the verification process may break
 * ***************************************************/

Processing: image.png…

url :https://freecodecam-boilerplate-vhpmzax9w5i.ws-eu115.gitpod.io

you should have a file called .env in your root path with the MESSAGE_STYLE defined in it. (it should not be called process.env)

also you should not have edited the server.js file
your code should be in myApp.js

1 Like

thank you, problem resolt.
The problem was my env file, i called it “process.env” and not onliy “.env”

1 Like