Can't get Use the .env File to pass

I can’t get the following code to pass:

app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === "uppercase")
  {
    return res.json({"message": "HELLO JSON"});
  }
    return res.json({"message": "Hello json"});
})

and am experiencing the following error:

npm start

> fcc-learn-node-with-express@0.1.0 start /home/runner/boilerplate-express
> node server.js

/home/runner/boilerplate-express/myApp.js:15
app.get('/json', function(req, res) {
^

ReferenceError: app is not defined
    at Object.<anonymous> (/home/runner/boilerplate-express/myApp.js:15:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/runner/boilerplate-express/server.js:7:13)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
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-04-21T19_58_50_772Z-debug.log
exit status 1

I have tried multiple variations of the same app.get function and am not sure what is throwing this error. Any help would be appreciated. Thank you in advance.

Your project link(s)

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

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

When I look at your code, I see the following:

/*var express = require('express');
var app = express();

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
});

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

//Create API
app.get("/json", (req, res) => {
  res.json({"message": "Hello json"});
});*/

app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === "uppercase")
  {
    return res.json({"message": "HELLO JSON"});
  }
    return res.json({"message": "Hello json"});
})

You have commented out code that is needed for the app to run correctly. You should look at the Node console to see the error message. Also, you have two ``app.getfor the/json` rout with different callback functions. The first one is the only one that will execute.

1 Like

I had to comment out my first app.get() function in order for the code to pass. Thank you for your input.

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