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

jason limmm
jason limmm
8,004 Points

sent to error page even thought there is no error?

const express = require('express');
const app = express();
const port = 3000;
const bodyparser = require('body-parser');
const cookieparser = require('cookie-parser');

app.use(bodyparser.urlencoded({extended: false}));
app.use(cookieparser);

app.use((req, res, next)=>{
    const err = new Error('Uh noes!');
    next(err);
    err.status = 500;
});

app.set('view engine', 'pug');

app.listen(3000, ()=>{
    console.log(`app is listening on port ${port}`);
});

const mainroute = require('./routes');
app.use(mainroute);

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

here is my code i keep being sent to the error page and now that i added the middleware with 4 parameters my website won't even load at all.

although i am not sure if this code does have an error even then that doesn't explain why my website isn't loading in

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey jason limmm 👋

The main issue is the placement of your error-creating middleware. It's currently placed before any routes, which means it will always create an error for every request.

app.use((req, res, next)=>{
    const err = new Error('Uh noes!');
    next(err);
    err.status = 500;
});

This middleware is creating an error for every request and passing it to the next middleware, which is why you're always being sent to the error page.

Hope this helps