Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Getting Started with Express Creating a Route with Express

Sean Flanagan
Sean Flanagan
33,235 Points

app.js doesn't work

Hi. I watched 5 minutes 41 seconds of the video and tried running node app.js and still got the same pre-formatted message as before:

Cannot GET /

Here's my app.js:

const express = require("express");

const app = express();

app.get("/", (request, response) => {
  response.send("I love Treehouse!");
});

app.listen(3000);

I saved app.js, made sure I was in the right folder, ran node app.js, then refreshed the browser and got the previous message. How do I get the response.send message please?

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Sean. I can confirm your code is correct and it works for me, but I cannot explain why not for you. I can actually 'comment out' lines 5, 6 & 7, save the file, CTRL+C to stop the server, type node app.js to start it again, then refresh the browser localhost:3000 to get back to the error you see, then uncomment those 3 lines, repeat the above process and get to see I love Treehouse!

I am using Chrome (Version 65.0.3325.146 (Official Build) (64-bit)), and when Cannot GET /, is being displayed and unlike for Andrew, I don't see a matching error message in the devtools console AFTER a single refresh. I have to look under Network to see it.

Sometimes in cases like these, one might need to to do a forced browser reset, the exact command can vary from browser to browser. In Chrome and with devtools selected, it is CTRL+SHFT+R.

Good luck with finding a solution.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Neil. Thanks for your input. I did what you suggested, a forced browser reset, by opening Chrome and using the Dev Tools, but the result was the same.

Thanks for trying.

Neil McPartlin
Neil McPartlin
14,662 Points

There is so little to this test, it is odd that it is not working for you. As you say, you have made sure that you are actually editing the same app.js file that you then go on to use with node app.js. Your node server must be running otherwise you would not be seeing Cannot GET /

I am using Windows 7 and these are the relevent code versions I see. Just type these into your terminal.

npm -v

5.7.1

node -v

v6.11.0

npm list express

flashcards@1.0.0 D:\TTH\Express\Andrew\flashcards\S1V5_Start
`-- express@4.16.3

If you are a Mac user, I do have access to one so let me know and I can try it on there too.

Hopefully others having more knowledge might be able to suggest some pertinent tests.

Neil McPartlin
Neil McPartlin
14,662 Points

Sean. 2 more thoughts.

1: In Chrome devtools console, do you see any additional error messages, apart from the expected 404 warning? Assuming not...

2: Given the installation is quite small with only one dependency (Express), why not delete and re-install. In the same folder where your app.js is located...

  • Delete package-lock.json (not to be confused with package.json).
  • Delete the node_modules folder.
  • Type npm install.
  • Type node app.js.

Now check your browser at http://localhost:3000/

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Neil.

In the console, I got two error messages;

Failed to load resource: the server responded with a status of 404 (Not Found)
:3000/favicon.ico 
Failed to load resource: the server responded with a status of 404 (Not Found)
localhost/
Neil McPartlin
Neil McPartlin
14,662 Points

Hi Sean. It does seem odd how it has separated the requested URL into 2 different errors. Seeing the message about the missing favicon icon makes me wonder whether your Chrome browser has a cookie containing information about a previous project you had running that used localhost:3000.

To see if the problem is specific to your Chrome browser, how about copying http://localhost:3000/ into Firefox and/or IE/Microsoft Edge to see if they work or display the same problem?

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Neil.

I tried to sign into Treehouse on Firefox but I got a message about some kind of internal error.

I tried Microsoft Edge but the "Cannot GET /" showed itself again.

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Sean. You mention not being able to login to Treehouse using Firefox but isn't the Express server and your code all running local on your computer? I'm guessing you are on Windows 10 given you have access to Edge. If you do have this running on Treehouse Workspaces, please feel free to make a snapshot and share here.

https://teamtreehouse.com/library/previews-and-snapshots

But if you do have everything installed locally, I do recommend my previous suggestion of doing a re-install as there is a real chance your previous Express module install may have been corrupted.

12 Answers

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Neil.

Unfortunately Workspaces doesn't seem to be available for this course. There are quite a few other courses that could do with Workspaces but also don't have it. I don't understand why.

I tried localhost:3000/ on Firefox but got the same output.

James Churchill
STAFF
James Churchill
Treehouse Teacher

Hi Sean!

Sorry to hear about the trouble you're having getting this example to run! I know how frustrating it can be when you can't get something to work properly.

Can you please confirm if you're using Workspaces in the browser or your local dev environment on your computer.

Thanks ~James

Sean Flanagan
Sean Flanagan
33,235 Points

Hi James. Sorry to drag you away from your other duties.

I'm using the local development environment because Workspaces isn't available for this course.

James Churchill
James Churchill
Treehouse Teacher

Are you using node app.js to run the file directly? Or are you using nodemon?

Sean Flanagan
Sean Flanagan
33,235 Points

The cd command produces a directory called Treehouse, which is where node app.js is. When I run dir, the output is:

Volume in Drive C has no label.
Volume Serial Number is 1809-B0BB

Directory of C:\Users\users\treehouse

10/03/2018 00:49     <DIR>     .
10/03/2018 00:49     <DIR>     ..
10/03/2018 00:47                    82 app.js
                          1 File(s)           82 bytes
                         2 Dir(s)            793,784,967,168 bytes free

I'm using Brackets. I've noticed that line 1 of my node app.js code has a red cross next to it indicating a parsing error, saying that the keyword const is reserved.

James Churchill
James Churchill
Treehouse Teacher

Where is your node_modules folder and package.json file located?

Sean Flanagan
Sean Flanagan
33,235 Points

Success!

The command prompt says Listening on port 3000 and when I go to localhost:3000 I get the output "I love Treehouse".

Thank you James. I appreciate all you've done and sorry for all this trouble. I think you deserve a nice long break after this. Have a lovely weekend.

And thank you Neil too for your support.

Sean :-)

James Churchill
James Churchill
Treehouse Teacher

That's awesome news Sean! I'm glad to hear that you were able to work it out :)

I can't tell you how many times this particular issue has bitten me. There's nothing more frustrating then making a change to a file and then not seeing that change take effect. It's enough to drive a person crazy!

Thanks for being a student and I hope you enjoy the rest of the course.

~James

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Sean. You are welcome and credit to James for his meticulous approach. Our choice of code editor often comes down to simple personal preference. As a Windows user I opted for Microsoft's Visual Studio Code (VSC) and I even use this on my Mac too. One feature I use all the time is that when I open a project locally, I always open a terminal (or terminals) within VSC meaning that I am always working in the correct folder. I just downloaded Brackets expecting to find a way to do this in there too. It maybe possible but I couldn't find it. In fact, reading the help notes it does say... "Using your own backend — Make sure your local server is already running, serving files from the same folder Brackets is editing" which suggest to me that perhaps it is not possible. i.e. the pressure is on you to ensure you are using the same folder in 2 separate apps. This is not such an overhead if you just have one project open, but once you have 2 or 3, confusion can creep in.

You also posed the question why some Treehouse courses do not come with Workspaces populated. It could be James is better placed to answer this but one issue I can see is that with node projects where one can be installing multiple npm packages, there could be a diskspace and/or performance issue for Treehouse workspaces and they would prefer the students work on the code locally. That said, you can still create your own Workspace and choose 'Node.js' for 'Pick an environment to start from'. You will then find that 'Pick an existing workspace or template to start from' automatically gets populated with 'Node.js Workspace'

Happy coding :)

Sean Flanagan
Sean Flanagan
33,235 Points

I'm using the Node.js command prompt.

James Churchill
James Churchill
Treehouse Teacher

Please try changing your app.js file to this:

const express = require("express");

const app = express();

app.get("/", (request, response) => {
  response.send("I love Treehouse!");
});

app.listen(3000, () => console.log('Listening on port 3000!'));

Save the app.js file and run node app.js from the command line. Then let me know what you see in the console and try to access your app using http://localhost:3000/.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi James. The console is empty. When I try http://localhost:3000/, I just get a message on the page saying

This site can’t be reached
localhost3000’s server IP address could not be found.
Try:

Checking the connection
Checking the proxy, firewall, and DNS configuration
Running Windows Network Diagnostics
ERR_NAME_NOT_RESOLVED
James Churchill
James Churchill
Treehouse Teacher

Interesting! You should have seen in the console the output "Listening on port 3000!".

For the next step in the debugging process, I'd like to confirm that your current directory in the console contains the files that you're editing in your code editor. If you run the cd command, what is your current directory? If you run the dir command, do you see the files you expect?

Also, what are you using for a code editor?

Sean Flanagan
Sean Flanagan
33,235 Points

They're both located in the Treehouse folder.

James Churchill
James Churchill
Treehouse Teacher

If that's the case, I would have expected to have seen them in the dir command output. Here's what I get in the console when I run the dir command:

C:\Users\james\Desktop\test>dir
 Volume in drive C is Windows
 Volume Serial Number is 00C1-AED2

 Directory of C:\Users\james\Desktop\test

03/16/2018  03:15 PM    <DIR>          .
03/16/2018  03:15 PM    <DIR>          ..
03/16/2018  03:16 PM               213 app.js
03/16/2018  03:14 PM    <DIR>          node_modules
03/16/2018  03:14 PM            13,039 package-lock.json
03/16/2018  03:15 PM               266 package.json
               3 File(s)         13,518 bytes
               3 Dir(s)  276,728,557,568 bytes free

If you're not seeing the node_modules folder and the package.json file in the same folder as your app.js file, I'd try running the following commands:

npm init -y
npm install express

Then run your app again with:

node app.js

After your app has started, you should see "Listening on port 3000!" output to the console. Then open a browser and browse to "http://localhost:3000/".

Sean Flanagan
Sean Flanagan
33,235 Points

Hi James. app.js is in the same folder as node_modules and package.json.

James Churchill
James Churchill
Treehouse Teacher

Sorry my confusion, but in a previous post, you showed the output of the dir command as:

Volume in Drive C has no label.
Volume Serial Number is 1809-B0BB

Directory of C:\Users\users\treehouse

10/03/2018 00:49     <DIR>     .
10/03/2018 00:49     <DIR>     ..
10/03/2018 00:47                    82 app.js
                          1 File(s)           82 bytes
                         2 Dir(s)            793,784,967,168 bytes free

I'm not seeing the node_modules folder or the package.json file in that output. I'm not sure why you wouldn't see those items listed in the dir command's output if they were actually there.

If you open the path C:\Users\users\treehouse in File Explorer, what do you see listed for the contents of the treehouse folder?

Sean Flanagan
Sean Flanagan
33,235 Points

In File Explorer, I see the node_modules folder, app.js, package.json and package-lock.json.

James Churchill
James Churchill
Treehouse Teacher

From the command prompt, could you please run the dir command again and reply back with all of the output? Also, try running more package.json, and include the output of that command as well.

Thanks so much for your patience!

Sean Flanagan
Sean Flanagan
33,235 Points

Thank you for going the extra mile.

Output from running dir:

Volume in Drive C has no label.
Volume Serial Number is 1809-B0BB

Directory of C:\Users\users\treehouse

10/03/2018 00:49     <DIR>     .
10/03/2018 00:49     <DIR>     ..
10/03/2018 00:47                    82 app.js
                          1 File(s)           82 bytes
                         2 Dir(s)            793,784,967,168 bytes free

Same as before.

Output of running more package.json:

Cannot access file C:\Users\user\treehouse\package.json
James Churchill
James Churchill
Treehouse Teacher

Okay, I believe that your current folder in the command prompt window isn't the SAME folder that you're viewing in File Explorer. The path of the folder in your command prompt window is "C:\Users\user\treehouse\" (taken directly from the output you gave in your previous post). Please double check that path against the path that's displayed in your File Explorer window. They should match.

My guess is that you've been editing the files that you see in File Explorer using Brackets, but running Node in the command prompt window against a different app.js file.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi James. Unfortunately the two paths don't match.

The one on File Explorer is:

This PC > Desktop > treehouse

That's the one containing the

It turns out that there's another folder, also called treehouse, in the user folder, and this treehouse folder has a file called app.js.

James Churchill
James Churchill
Treehouse Teacher

From the command prompt window, you can run the command cd C:\Users\users\Desktop\treehouse (guessing the path a bit there) to browse to the folder that's showing in File Explorer.

You can also open a command prompt window from File Explorer. Here are some of the ways to do that: http://www.zdnet.com/article/windows-10-tip-the-fastest-smartest-ways-to-open-a-command-prompt/