freeCodeCamp: Basic Node and Express - Use .env file

Tell us what’s happening:
Hi, I’m newbie and start learning Nodejs on freeCodeCamp. I’m at the lesson “” and get stuck there. Please help me check my code and correct it. Thanks.

Your code so far

var express = require('express');
var app = express();

console.log("Hello World");
app.get("/json", function(req, res) {
  if (process.env.MESSAGE_STYLE === "uppercase") {
  res.send("{message: " + "Hello Json".toUpperCase() + "}");
  } else {
  res.send("{message: Hello Json");
app.use(express.static(__dirname + "/public"));
module.exports = app;

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

Have you included a .env file in the root of the project?

To be clear, the .env file is literally called .env, that’s not an extension, it’s the whole name - that catches people out sometimes…

Welcome, there.

I recommend you go over the previous lesson:
Basic Node and Express - Serve JSON on a Specific Route | Learn |


Inside the route handler, use the method res.json() , passing in an object as an argument. This method closes the request-response loop, returning the data. Behind the scenes, it converts a valid JavaScript object into a string, then sets the appropriate headers to tell your browser that you are serving JSON, and sends the data back. A valid object has the usual structure {key: data} . data can be a number, a string, a nested object or an array. data can also be a variable or the result of a function call, in which case it will be evaluated before being converted into a string.

Hope this helps

I did. Thanks Jackson.

I did a mistake using the method res.send(). Thanks.

Can anyone else help me with this problem? I cant get my code to pass, even though when i go to ‘my-app-url/json’ it displays '{“message”: “HELLO JSON”} as the exercise specifies. I’ve been at this for a few days, and have tried every method i can think of to display ‘{“message”: “HELLO JSON”}’. My .env file is also named just ‘.env’ in the root(under the views tab, where myApp.js is located, correct me if my understanding of the root is incorrect). Does anyone have any ideas I might not be thinking of? Here is my code at the moment:
var express = require(‘express’);
var app = express();

app.get(’/’, function(req, res) {
//retrieve path of HTML file.
absolutePath = __dirname + ‘/views/index.html’ ;

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

app.get(’/json’, function(req,res) {
var response = “Hello json”.toUpperCase();
if (process.env.MESSAGE_STYLE === “uppercase”) {
res.json({“message”: “Hello json”.toUpperCase()});
} else {
res.json({“message”: “Hello json”});

if (process.env.MESSAGE_STYLE === “uppercase”) {
response = “Hello json”.toUpperCase();
} else {
response = “Hello json”;
res.json({“message”: response});


//app.use(express.static(__dirname + “/public”));
module.exports = app;

console.log(“Hello World”);

The url is as follows: