Basic Node and Express - Use the .env File

Tell us what’s happening:

I have been stuck in this step for a while, i have created the .env file with the accurate variable i also asigned require(‘dotenv’).config() at the top of the myApp.js. I need help here please. Here’s my code

require('dotenv').config();
let express = require('express');
let app = express();
/* app.get('/', function(req, res) {
  res.send('Hello Express');
}); */

app.get('/', (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
});

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

app.get('/json', (req, res) => {

  if (process.env.MESSAGE_STYLE === "uppercase") {
    res.json({ "message": "HELLO JSON" });
  } else {
    res.json({ "message": "Hello json" });
  }
});
console.log("Hello World");

###Your project link(s)

solution: http://localhost:3000

Your browser information:

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

Challenge Information:

Basic Node and Express - Use the .env File

Your code is missing the command to start the server. Do you know how do you do so?

is it npm run start?

app.listen() takes a number that starts your server at that port. You can access it by http://localhost:portNumber. Another thing to note that is you are comparing UPPERCASE to uppercase (if (process.env.MESSAGE_STYLE === “UPPERCASE”)) that is why you will see Hello json

i start the server using git bash. Thats what ive been using to start and stop the server. or is it something else you are talking about cause i dont quite understand

can I see the entire code? What have you been doing? It is difficult to tell without seeing the output/code

Basically thats all the code i have in the myApp.js and this in .env file i created

MESSAGE_STYLE=uppercase

Here’s what you need to do:

  • Start the server at a port by using app.listen(number)
  • Go to /json path. If you are comparing uppercase === uppercase you should see HELLO JSON

Did you try this? The package.json file for boilerplate-express has that script already set up:

 "scripts": {
    "start": "node server.js"
  }

So npm run start should work as long as you’re in the directory where boilerplate-express lives.

I got it working now. I think the problem was from the server i had to stop it and restart it again multiple times. Thank you