Use the .env File crashing Replit server

I have written everything correct and getting the result when accessing /json on browser, but my server is crashing when I am submitting the Replit link on the course module

I have created the key in secret tab as
key: MESSAGE_STYLE
value: uppercase

My code:

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


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


app.get('/',function(req,res) {
	res.sendFile( __dirname + '/views/index.html')
})

app.get('/json', function (req,res) {
	
	if (process.env.MESSAGE_STYLE == "uppercase") {
		response = "Hello json".toUpperCase();
	} 
	else {
		response = "Hello json";
	}

	res.json({"message" : response})
})

The error message on console :

 npm start

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

Node is listening on port 3000...
*
*
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 (node:events:394:28)
    at IncomingMessage.Readable.read (node:internal/streams/readable:527:10)
    at flow (node:internal/streams/readable:1012:34)
    at resume_ (node:internal/streams/readable:993:3)
    at processTicksAndRejections (node:internal/process/task_queues:83: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 (node:events:394:28)
    at IncomingMessage.Readable.read (node:internal/streams/readable:527:10)
    at flow (node:internal/streams/readable:1012:34)
    at resume_ (node:internal/streams/readable:993:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
node:_http_outgoing:573
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:573: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 (node:events:394:28)
    at IncomingMessage.Readable.read (node:internal/streams/readable:527:10)
    at flow (node:internal/streams/readable:1012:34) {
  code: 'ERR_HTTP_HEADERS_SENT'
}
exit status 1

solution: boilerplate-express - Replit

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0

Challenge: Use the .env File

Link to the challenge:

Having the same error.

You have changed your code since your last post.

I see:

app.get("/json", 
	const message = "Hello json";
    (req, res) => res.json(
        {"message": process.env.MESSAGE_STYLE === "uppercase" ?
			message.toUpperCase() : message
    })
);

You have a major syntax issue here. The second argument of app.get should be a function. You do not have a properly formed function.

Yes, earlier my code was same as the above, I changed it later after reading some solutions and copied it without checking

Check now

https://replit.com/@YASHBRO/boilerplate-express#myApp.js

@lasjorg I forked this user’s replit. I deleted the node_modules folder and ran npm install. I copied/pasted my full working code, clicked Run and when I tried to submit the project url, I am seeing the following in the console. What is causing this and how to resolve?

@RandellDawson

I did refer to these thread, but it didn’t help me either

SOLVED!

I ran the following command in the console/terminal, and it worked without changing any of my previous code :

npm update --save
npm ci

Not sure which one solved the issue because I ran it consecutively and tested the solution on freeCodeCamp after it

1 Like