Error Basic Node and Express - Use the .env File

I have a challenge solution but it doesn’t work due to :

SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse ()
and
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client error.

How can i fix this?

glitch - https://glitch.com/~try-nodejs

Challenge: Use the .env File

Link to the challenge:

1 Like

try a code like this:

let myMessage="message content";
res.json({
message:myMessage
})

your code was fine at the first time, I just wanted to tell you to use message instead of "message"

the same error

app.use('/json', (req, res) => {
  let myMessage = "Hello json";
  
  if(process.env.MESSAGE_STYLE === 'uppercase') {
    return res.json({"message": myMessage.toUpperCase()})
  }else {
    return res.json({"message": myMessage})
  }
})

try without a "" quote mark with message
like {message:yourmessage} not like {"message":yourmessage}

it’s working thank you

1 Like

Same error here:

app.use('/json', (req, res) => {
  let myMessage = "Hello json";
  
  if(process.env.MESSAGE_STYLE === 'uppercase') {
    return res.json({message:myMessage.toUpperCase()})
  }else {
    return res.json({message:myMessage})
  }
})