Basic Node And Express Use the .env File Not Passing Test And Breaking Server

Tell us what’s happening:

The code works for me but I’m getting an error and the server breaks when submitting my solution.

The hint section said to do the following, which seems to be set up correctly.

NOTE: Ensure the fcc-express-bground package in your package.json file is pointing to this URL: https://github.com/freeCodeCamp/fcc-express-bground-pkg.git

My /json Route

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

Error:

https://www.freecodecamp.org
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:91:26
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:314:20)
    at IncomingMessage.Readable.read (_stream_readable.js:507:10)
    at flow (_stream_readable.js:1007:34)
    at resume_ (_stream_readable.js:988:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:99:28
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:314:20)
    at IncomingMessage.Readable.read (_stream_readable.js:507:10)
    at flow (_stream_readable.js:1007:34)
    at resume_ (_stream_readable.js:988:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
_http_outgoing.js:530
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:530:11)
    at ServerResponse.header (/home/runner/boilerplate-express/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/home/runner/boilerplate-express/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/runner/boilerplate-express/node_modules/express/lib/response.js:267:15)
    at /home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:108:15
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:314:20)
    at IncomingMessage.Readable.read (_stream_readable.js:507:10)
    at flow (_stream_readable.js:1007:34)
    at resume_ (_stream_readable.js:988:3) {
  code: 'ERR_HTTP_HEADERS_SENT'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fcc-learn-node-with-express@0.1.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the fcc-learn-node-with-express@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-03-09T21_30_37_838Z-debug.log
exit status 1

Your project link(s)

solution: https://boilerplate-express.danielfloyd.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

I just found this information. I did the required steps and now the server isn’t breaking when submitting, but the test isn’t passing.

Take a closer look at the if condition, and at the instructions when message should be uppercase.

Hmmm. Thanks for the tip. I changed my code to the following:

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

I’m still unsure as to why it passes, compared to my old code. With my previous code if the environment variable wouldn’t be there it should return undefined. My understanding was going to be if the environment variable was undefined it would skip the if and go to the else. Especially since when I test it locally it works completely fine, so I’m unsure as to how they do their test.

Ah, but the tests change process.env.MESSAGE_STYLE to be "lowercase". They do not remove the variable.

Hope this clarifies

1 Like

Makes sense. Now that I have re-read the question again I do see the following part.

transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase.

This obviously clarifies the reason for the equal to, since they could overwrite my environment variable to be any value which explains why my original condition was failing.

Thanks for the help.