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 trialAva Jones
10,723 PointsI have no idea why localhost isn't working
my code:
const express = require('express'); const fs = require('fs'); const path = require('path');
const app = express();
app.set('view engine', 'pug'); app.set("views", path.join(__dirname, "views")); app.use(express.static('public'));
//CALL BACKS function getUsers(cb){ fs.readFile('data.json', 'utf8', (err, data) => { if (err) return cb(err); const users = JSON.parse(data); return cb(null, users); }); }
app.get('/', (req,res) => { getUsers((err, users)=>{ if(err){ res.render('error', {error:err}); } else { res.render('index', {title: "Users", users: users.users}) } }); });
app.listen(3000, () => console.log('App listening on port 3000!'));
3 Answers
Travis Alstrand
Treehouse Project ReviewerSo I just downloaded the project files from the video you mentioned. I do see that there are separate folders for each video throughout the course (V2
= Video 2, etc...)
So if you're on video 2 and you open the entire folder within your Text Editor, and open up a terminal, you should be at the root folder /async_express_projectfiles
.
You'll want to use the command cd V2_starter_files_callbacks
(you can probably use Tab Completion to not have to type all that out). This will get you in the correct folder within your terminal.
Then you'll need to use the command npm install
or npm i
for short to install all the dependencies needed to run the app. Don't worry about "warnings" or "deprecated warnings", everything should still work fine. What you posted above is normal.
Finally, running npm start
should kick off the app and you should see
[nodemon] starting `node app.js`
App listening on port 3000!
Are you still not seeing this? The only thing I can think of with what you provided is that you're not in the correct directory when running npm start
if that's how your package.json
file looks. You can always delete the downloaded folder and re-download it if you think maybe something was edited or changed by mistake.
For future videos in this course you can either
- Continue working in the first folder you start in (recommended)
- Repeat these steps in the next
V<number>
folder (changing directories to it, installing deps, starting server)- If you do this, you can either stop the current server
CTRL + C
and close the terminal, then open a new one or - Stop the current server, then back up a directory
cd ..
thencd
into the next folder.
- If you do this, you can either stop the current server
Ava Jones
10,723 PointsThank you so much for your help this worked immediately!!!
Travis Alstrand
Treehouse Project ReviewerOh good! I'm so glad you were able to get it working! Nice job!! 😃
Travis Alstrand
Treehouse Project ReviewerThe code you have seems to be alright from what I can see. I'm not sure what video this is from to check the project files out but in your terminal, make sure you've changed directories to the correct folder using the cd
command.
If there are a lot of folders within the project files it can be confusing sometimes but ensure you're at the root of the current project. Then make sure you've installed the dependencies with npm install
. After that, running the start command used in the video should get it going for you. Make sure you see your message in the terminal after running it, App listening on port 3000!
. If you don't see this, then either you're probably in the wrong directory or haven't installed the dependencies. If you see any error messages in your terminal after trying to run it, please share them with us as it's hard to know exactly what's going on just from seeing this one file's code 👍
Ava Jones
10,723 Pointsok everything works perfectly when i type cd into the terminal but when i type npm install i get this:
up to date, audited 145 packages in 538ms
20 packages are looking for funding
run npm fund
for details
3 vulnerabilities (2 moderate, 1 high)
To address issues that do not require attention, run: npm audit fix
To address all issues (including breaking changes), run: npm audit fix --force
Run npm audit
for details.
And this is what i get when i type npm start:
npm ERR! Missing script: "start" npm ERR! npm ERR! Did you mean one of these? npm ERR! npm star # Mark your favorite packages npm ERR! npm stars # View packages marked as favorites npm ERR! npm ERR! To see a list of scripts, run: npm ERR! npm run
npm ERR! A complete log of this run can be found in: /Users/avajones/.npm/_logs/2024-08-15T19_34_15_028Z-debug-0.log
Also the video is called Using callbacks by treasure porth
Ava Jones
10,723 PointsAnd it still doesn't work
Travis Alstrand
Treehouse Project ReviewerCan you link to the video you're on so I can download the project files and look? Or copy/paste your package.json
file here for us to inspect?
Ava Jones
10,723 PointsSorry for the very late response here is the package.json:
{ "name": "async-express", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "start": "nodemon" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.16.4", "pug": "^2.0.3" }, "devDependencies": { "nodemon": "^1.18.9" } }
I cannot copy the link for the video but here is the name of the course and the video title:
course -> Asynchronous Code in Express
video -> Using callbacks
Ava Jones
10,723 PointsAva Jones
10,723 Pointsthe code that i shared above is the app.js file just to clarify