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 Serving Static Files in Express Merging the Design Files

Anyone willing to help find my error? My flashcard app keeps crashing and i cant understand the error.

I just completed the express course and was coding along throughout. Everything was working great until the last few steps. I tried copying the course files to compare with my project and Im not sure what I did but now my app crashes and gives a throw error that I cant decipher. Would be SO appreciative if anyone would be willing to take a look at my code and help me fix it. I've worked hard on this and was so excited to customize and make it my own!

my flashcard repo: https://github.com/petitlapin86/express-flashcards

the error i'm getting: module.js:471 throw err; ^

Error: Cannot find module './routes' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object.<anonymous> (/Users/paigeskinner/flashcards/app.js:13:20) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3)

3 Answers

Hi Paige,

An error is being thrown within your hello.pug and index.pug files. It is trying to load a file called app.pug, that doesn't exist.

 extends app.pug

The error is reaching your error handler:

app.use((err, req, res, next) => {
  res.locals.error = err;
  res.status(err.status);
  res.render('error');
});

But it doesn't contain a status code, err.status is undefined, so the line:

res.status(err.status);

Becomes:

res.status(undefined);

Which is invalid. Hence the "RangeError: Invalid status code: 0 at ServerResponse.writeHead" - the server can't write a response header using the status code, because the status code is undefined.

I changed your error handler to:

app.use((err, req, res, next) => {
  res.locals.error = err;
  console.log("Error Status: ", err.status);
  res.status(err.status || 500);
  res.render('error');
});

So now if there is no status code, we set a default one of 500.

Doing that gave me this error:

Error: ENOENT: no such file or directory, open 'C:\Users\James\Downloads\express-flashcards-master\express-flashcards-master\views\app.pug'

Indicating that you are trying to open a file called app.pug that doesn't exist.

And that's what brought me to your hello.pug and index.pug files. If you remove:

 extends app.pug

Your app seems to work.

I would recommend swapping the error handler for the one I posted first. So you can see what's going on. Then remove the extends app.pug line to fix the error.

Hope this helps :)

Thank you so much! You helped me see what I was missing! In the last step of the course he has us create an app.pug file and the card.pug and index.pug files extend app.pug -> which extends layout. I missed the step of making the app.pug file and it was throwing me off! Yay all working now!

I had a totally different error - but this allowed me to diagnose the problem - using a better error handler showed me that the indentation on my app.pug file was incorrect for the block card portion and boom - fixed the issue.

Hi Paige,

I think you have accidentally moved your 'routes' folder into the 'public' folder. Just put it back into the root of your project, and that should fix it.

File structure should look like this

Hope this helps :)

James Anwyl thanks so much -ive fixed that mistake but i am still getting an error when i navigate to localhost:3000

RangeError: Invalid status code: 0 at ServerResponse.writeHead (http_server.js:192:11) at ServerResponse._implicitHeader (_http_server.js:157:8) at ServerResponse.OutgoingMessage.end (_http_outgoing.js:573:10) at ServerResponse.send (/Users/paigeskinner/flashcards/node_modules/express/lib/response.js:221:10) at done (/Users/paigeskinner/flashcards/node_modules/express/lib/response.js:1004:10) at Object.exports.renderFile (/Users/paigeskinner/flashcards/node_modules/pug/lib/index.js:421:12) at View.exports._express as engine at View.render (/Users/paigeskinner/flashcards/node_modules/express/lib/view.js:135:8) at tryRender (/Users/paigeskinner/flashcards/node_modules/express/lib/application.js:640:10) at EventEmitter.render (/Users/paigeskinner/flashcards/node_modules/express/lib/application.js:592:3)

any suggestions? thanks again for taking the time to help - i am so appreciative!