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
Rohan Kapoor
294 Pointssomeone please explain me how this error handler working here in app.js
app.js
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var app = express();
//mongodb connection
mongoose.connect("mongodb://localhost:27017/bookworm");
var db = mongoose.connection;
//mongo error
db.on('error',console.error.bind(console,'connection error : '));
//use session for tracking logins
app.use(session({
secret: 'treehouse loves you',
resave : true,
saveUninitialized : false
}));
//make userId access in all templates
app.use(function(req,res,next){
res.locals.currentUser = req.session.userId;
next();
});
// parse incoming requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// serve static files from /public
app.use(express.static(__dirname + '/public'));
// view engine setup
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');
// include routes
var routes = require('./routes/index');
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
});
});
// listen on port 3000
app.listen(3000, function () {
console.log('Express app listening on port 3000');
});
index.js
router.post('/login',function(req,res,next){
if(req.body.email && req.body.password){
User.authenticate(req.body.email,req.body.password,function(error,user){
if(error || !user){
var err = new Error('Wrong email or password.');
err.status = 401;
return next(err);
}else{
req.session.userId = user._id;
return res.redirect('/profile');
}
});
}else{
var err = new Error('Email and password required.');
err.status = 401;
return next(err);
}
});
here in this route we have return next(err) . So what is next here from these below functions
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
});
});