Basic Node and Express - Use the .env File

Tell us what’s happening:
i created a .env file
but it was not possible to edit content.

therefore i did it local and upload this .env
hopefully it was overwritten.

is still cant see the contend of .env

and my test don’t work

as content of my .env i used:

MESSAGE_STYLE=uppercase

app code:

let express = require('express');
let app = express();
const path = require('path');
// console.log("Hello Express");
// console.log('__dirname=' + __dirname); // __dirname=/home/runner/boilerplate-express
app.get("/", (req, res) => {
  // res.sendFile('/views/index.html');
  res.sendFile(path.join(__dirname, '/views/index.html'));
});
app.get("/json", (req, res) => {
  res.json({
    message: "Hello json".toUpperCase()
  });
});
// Assets at the /public route
app.use('/public', express.static(__dirname + '/public'));

module.exports = app;

Error:

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

Your project link(s)

solution: boilerplate-express - Replit

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0

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

Link to the challenge:

You always send HELLO JSON back as the response no matter what the MESSAGE_STYLE environment variable says.

1 Like

oh thats true. BTW seems my .env is always deleted? how could i let it stay for ever?

Error is still the same, but i sthink the source looks good:

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

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