Basic node and express problem

i can not get my code to run on repl. if i change it to app.get(‘/’) it works on repl but freecodecamp wont accept answer. can anyone see where i am going wrong.

app.get('/json', (req, res) => {
if(process.env['MESSAGE_STYLE'] === mySecret){
  res.json( {"message": "Hello json".toUpperCase()})
}else{
  res.json({"message": "Hello express"})
}
  
})

Post your Replit link.

res.json({"message": "Hello express"})

is this what you are asked to send?

yes and it sends it on repl but only if i leave the app.get(‘/’, function) like that, but freecodecamp wont accept it. i had to use repl secret file which created the mySecret variable

https://boilerplate-express.dowo.repl.co

I assume it’s this challenge?

If so, re-read the requirements.

  1. You are using the wrong value for the environment variable.

  2. You are not sending what is asked for.

  3. The GET route should be /json not /. But you submit the root URL (the URL you posted).

Note: If you are using Replit, you cannot create a .env file. Instead, use the built-in SECRETS tab to add the variable.

yes i tried sending it from the app.get(‘/json’) but repl retuurns not found error. below is the code i have. if i use that code then repl finds it and prints the message as it should but when i try to submit it to freecodecamp it tells me that i need to do what you said,.
but then repl returns not found error. I dont understand why. can you see anything wrong in my code? i had to use the Secrets tab on repl as described above for the env variable, the value of mySecrets var is ‘uppercase’.

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

const absolutePath = __dirname + ‘/views/index.html’;
console.log(‘hello world’)
const mySecret = process.env[‘MESSAGE_STYLE’]
// app.get(“/”, (req, res) => {

// res.sendFile(absolutePath);
// });

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

app.get(‘/’, (req, res) => {
if(process.env[‘MESSAGE_STYLE’] === mySecret){
res.json( {“message”: “Hello json”.toUpperCase()})
}else{
res.json({“message”: “Hello express”})
}

})
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

As @lasjorg says, your route should be ‘/json’, and you don’t need to comment out the ‘/’ route.

Also, this line:

if(process.env[‘MESSAGE_STYLE’] === mySecret){

…is equivalent to

if(process.env[‘MESSAGE_STYLE’] === process.env[‘MESSAGE_STYLE’]){

…as that is what is assigned to the variable mySecret.
You should be checking if the value assigned to MESSAGE_STYLE is ‘uppercase’.

Again, as pointed out, ‘Hello express’ is not a value which should be returned in any case in this challenge.

this is what i have on repl - is that not what the challenge should show?

if i change the app.get(‘/json’) i get not found. i don’t see why

this is the mySecret var. i’m just not getting it, sorry to be a pain

The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"} , depending on the MESSAGE_STYLE value. Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.

Note: If you are using Replit, you cannot create a .env file. Instead, use the built-in SECRETS tab to add the variable.

am i doing the wrong thing completely then? oh sorry ive just seen that im not sending hello json

so sorry everyone, its passed now. sorry for wasting everyones time. thanks

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