Tell us what’s happening:
When applying the unit test for this lesson, my repl.it server fails with what appears to be a server side error originating at FCC’s verification process. In my repl.it console I can see the request come in and an error is generated.
Repl.it console error:
npm start
fcc-learn-node-with-express@0.1.0 start /home/runner/FCC-express-lessons
node server.jsNode is listening on port 3000…
*
GET / - ::ffff:172.18.0.1
*
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse ()
at /home/runner/FCC-express-lessons/node_modules/fcc-express-bground/index.js:91:26
at IncomingMessage. (/home/runner/FCC-express-lessons/node_modules/fcc-express-bground/index.js:33:7)
at IncomingMessage.emit (events.js:315:20)
at IncomingMessage.Readable.read (_stream_readable.js:505:10)
at flow (stream_readable.js:1005:34)
at resume (_stream_readable.js:986:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse ()
at /home/runner/FCC-express-lessons/node_modules/fcc-express-bground/index.js:99:28
at IncomingMessage. (/home/runner/FCC-express-lessons/node_modules/fcc-express-bground/index.js:33:7)
at IncomingMessage.emit (events.js:315:20)
at IncomingMessage.Readable.read (_stream_readable.js:505:10)
at flow (stream_readable.js:1005:34)
at resume (_stream_readable.js:986:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
_http_outgoing.js:518
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:518:11)
at ServerResponse.header (/home/runner/FCC-express-lessons/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/home/runner/FCC-express-lessons/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/runner/FCC-express-lessons/node_modules/express/lib/response.js:267:15)
at /home/runner/FCC-express-lessons/node_modules/fcc-express-bground/index.js:108:15
at IncomingMessage. (/home/runner/FCC-express-lessons/node_modules/fcc-express-bground/index.js:33:7)
at IncomingMessage.emit (events.js:315:20)
at IncomingMessage.Readable.read (_stream_readable.js:505:10)
at flow (stream_readable.js:1005:34)
at resume (_stream_readable.js:986: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-02-06T10_22_11_569Z-debug.log
I’ve had a look at the FCC’s verification process code here:
And the error appear to occur when parsing the response at line 91 and 99:
try {
lowerCase = JSON.parse(lowerCase).message;
} catch (e) {
console.log(e);
next(e);
}
try {
upperCase = JSON.parse(upperCase).message;
} catch (e) {
console.log(e);
next(e);
}
Here is the state of my .env file when I initiate the test:
MESSAGE_STYLE=uppercase
Here is a typical api response when MESSAGE_STYLE===undefined:
(taken from the browsers developer tools > Network > Response > Response Payload)
{"message":"Hello json"}
Here is a typical api response when MESSAGE_STYLE===uppercase:
(taken from the browsers developer tools > Network > Response > Response Payload)
{"message":"HELLO JSON"}
Thanks in advance.
Your code so far
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"))
app.use(function middleware(req, res, next) {
const logStr = `${req.method} ${req.path} - ${req.ip}`
console.log(logStr)
next()
})
app.get("/", (_,res) => {
const fileName = __dirname + "/views/index.html"
res.sendFile(fileName)
})
app.get("/json",(_,res) => {
let resJson = {message:"Hello json"};
if (process.env.MESSAGE_STYLE==="uppercase"){
resJson.message = resJson.message.toUpperCase()
}
return res.json(resJson)
})
module.exports = app;
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
.
Challenge: Use the .env File
Link to the challenge: