Basic Node and Express - Use the .env File

Tell us what’s happening:
Have trouble waking up and didn’t know if my code was the problem on replit.

Your code so far

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

console.log("Hello World")

// app.get('/', (req, res)=>{
//   res.send('Hello Express')
// })

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

// let message= {"message": "Hello json"}
const mySecret = process.env['MESSAGE_STYLE']

// app.get('/json',(req, res) => {
  // res.json(message)
// })
let message = {"message": "Hello json"}

app get('/json', req, res)=>{
 If(process.env.mySecret === "uppercase"){
   res.json({"message": "HELLO JSON"})
 }else{
   res.json(message)
 }
}


























 module.exports = app;

Your browser information:

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

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

Link to the challenge:

You initialize mySecret to be the value in the environment variable named MESSAGE_STYLE but then you have the following line:

 If(process.env.mySecret === "uppercase"){

Two issues here:

  1. You have a typo in the first keyword If
  2. You compare an environment variable named mySecret to "uppercase"???

The instructions do not say anything about creating an environment variable named mySecret .

ok, fix those but the npm still doesn’t start.

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

console.log("Hello World")

// app.get('/', (req, res)=>{
//   res.send('Hello Express')
// })

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

// let message= {"message": "Hello json"}


// app.get('/json',(req, res) => {
  // res.json(message)
// })
let message = {"message": "Hello json"}

app.get('/json', req, res)=>{
 if(process.env.MESSAGE_STYLE === "uppercase"){
   res.json({"message": "HELLO JSON"})
 }else{
   res.json(message)
 }
}


























 module.exports = app;

Need to see your full project code.

My repLIT

Got it to work but it’s not passing for some reason
image

Your package-lock.json file is not correct which is effecting the installation of a special package made to test the project.

  1. Click Stop on your app.

  2. Type npm install https://github.com/freeCodeCamp/fcc-express-bground-pkg.git and hit ENTER.

  3. After install completes, click Run again so the package-lock.json reflects the changes and the correct version of the package installs.

  4. Try submitting your live url (https://boilerplate-express-1.royalgreen.repl.co) again for the challenge (if that is the same project you are working on).

Looks like you made the changes, so you should be passing the challenge now.

FYI - A fix was found for all the boilerplates and we have been merging the necessary changes so all of these corrupt replits and non-installing replits should be fixed when importing the projects from GitHub into repl.it in the future.

that is good to hear. :grinning:

Look like the next one doing the same thing:

fcc-learn-node-with-express@0.1.0 start
node server.js

Hello World
Node is listening on port 3000…
*
GET / _ 74.211.3.138
*
GET /public/style.css _ 74.211.3.138
*
GET / _ 63.160.159.164
*
GET /public/style.css _ 63.160.159.164

*
GET /json _ 35.247.44.72

Should be Working
My repLIT

Which challenge is this for?

Basic Node and Express


Implement a Root-Level Request Logger Middleware

Look carefully at the format of the output expected:

GET /json - ::ffff:127.0.0.1

You are returning:

GET /json _ ::ffff:127.0.0.1

whoopsie hit the shift button when I :grinning: shouldn’t.