error middleware, closes #60

v1.18.x
Baris Usakli 12 years ago
parent 9b02fa0ab9
commit 47378fb978

@ -43,15 +43,16 @@ var user = require('./../user.js'),
res.send(app.build_header(res) + app.create_route("users-search", "users") + templates['footer']); res.send(app.build_header(res) + app.create_route("users-search", "users") + templates['footer']);
}); });
app.get('/users/:userslug', function(req, res) { app.get('/users/:userslug', function(req, res, next) {
if(!req.params.userslug) { if(!req.params.userslug) {
res.send("User doesn't exist!"); next();
return; return;
} }
user.get_uid_by_userslug(req.params.userslug, function(uid) { user.get_uid_by_userslug(req.params.userslug, function(uid) {
if(!uid) { if(!uid) {
res.redirect('/404'); next();
return; return;
} }
@ -60,6 +61,7 @@ var user = require('./../user.js'),
}); });
app.get('/users/:userslug/edit', function(req, res) { app.get('/users/:userslug/edit', function(req, res) {
console.log('derp');
if(!req.user) if(!req.user)
return res.redirect('/403'); return res.redirect('/403');

@ -59,6 +59,8 @@ var express = require('express'),
auth.initialize(app); auth.initialize(app);
app.use(app.router);
app.use(function(req, res, next) { app.use(function(req, res, next) {
// Don't bother with session handling for API requests // Don't bother with session handling for API requests
if (/^\/api\//.test(req.url)) return next(); if (/^\/api\//.test(req.url)) return next();
@ -73,6 +75,39 @@ var express = require('express'),
next(); next();
}); });
app.use(function(req, res, next) {
res.status(404);
// respond with html page
if (req.accepts('html')) {
//res.json('404', { url: req.url });
res.redirect('/404');
return;
}
// respond with json
if (req.accepts('json')) {
console.log('sending 404 json');
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
app.use(function(err, req, res, next) {
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
console.error(err.stack);
res.status(err.status || 500);
res.json('500', { error: err.message });
});
auth.create_routes(app); auth.create_routes(app);
admin.create_routes(app); admin.create_routes(app);
userRoute.create_routes(app); userRoute.create_routes(app);

Loading…
Cancel
Save