Basic Node and Express - Use the .env File

Tell us what’s happening:

Describe your issue in detail here.
I have tried different variations, starting the course from scratch, looking up other forum posts and I still can not get past this lesson.

this is the entire code I have right now in myApp.js on Replit:

require(‘dotenv’).config()

let express = require(‘express’);
let app = express();

/* app.get(“/”, function(req, res) {
res.send(“Hello Express”);
});
*/

app.get(“/”, function(req, res) {
res.sendFile(absolutePath = __dirname + “/views/index.html”);
});

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

app.get(“/json”, function(req, res) {
var mySecret = process.env[‘MESSAGE_STYLE’];
if (mySecret === “uppercase”) {
res.json({“message”: “HELLO JSON”})
} else {
res.json({“message”: “Hello json”})
};
});

Your project link(s)

Your browser information:

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

Challenge Information:

Basic Node and Express - Use the .env File

using square brackets with quotes to access environment variables, as in process.env['MESSAGE_STYLE'] , is not the conventional or recommended way to access them in Node.js.

The conventional and more readable way is to access the environment variables directly as process.env.MESSAGE_STYLE .

use console.log(process.env.MESSAGE_STYLE) and see whats in the console.

noted, thank you.

the console shows the word uppercase

1 Like

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