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 trialAmy gh
5,915 PointsCannot GET /quotes
const express = require('express'); const app = express();
app.get('/quotes', (req, res)=>{ res.json(data); });
app.listen(3000, () => console.log('Quote API listening on port 3000!'));
const data = { "quotes": [ { "id": 8721, "quote": "We must accept finite disappointment, but we must never lose infinite hope.", "author": "Martin Luther King" }, { "id": 5779, "quote": "Use what you’ve been through as fuel, believe in yourself and be unstoppable!", "author": "Yvonne Pierre" }, { "id": 3406, "quote": "To succeed, you have to do something and be very bad at it for a while. You have to look bad before you can look really good.", "author": "Barbara DeAngelis" } ] }
Question: run npm start in the terminal with no error then chrome http://localhost:3000/quotes Cannot GET /quotes
1 Answer
Loukas Piloidis
6,700 PointsHi Amy,
I copied the exact code that you have on your question and it worked fine on my browser, which means that the code itself is correct.
One possible solution might be that you haven't installed all the node.js dependancies required for this basic server. Trying to troubleshoot this way might help, although I don't think that's the case since your "npm start" command works.
In any case, try to run "npm init" command firstly, to initialize the required package.json file, and then install express using "npm install express". Make sure that the key "main" in package.json is set to the name of your .js file (index.js for example).
Finally, you need to manually restart the server and refresh the browser after every change you make. This step is very important. You can do this the following two ways:
After saving changes in your .js file, you press ctr+c in your terminal on mac (windows have the same hotkey I think), and then you restart the server again by typing "npm start". Then, refresh your browser, and voila!
There is a dependency called nodemon that I am using as well, that automatically refreshes your server every time you save a change in your .js file. You still need to refresh your browser though. To install it, run "npm install nodemon" in the terminal, and then, instead of running the server with "npm start" you will just type "nodemon" once, and be set. You can read more about this handy tool here https://www.npmjs.com/package/nodemon
I hope this helps :)