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.