Skip to content

Instantly share code, notes, and snippets.

@dhollenbeck
Last active November 14, 2016 11:27
Show Gist options
  • Save dhollenbeck/f963e49136df0089236f4e5fc1633d68 to your computer and use it in GitHub Desktop.
Save dhollenbeck/f963e49136df0089236f4e5fc1633d68 to your computer and use it in GitHub Desktop.
Express on delete redirect
// This gist shows how to handle multiple redirect options after a delete resource.
app.get('/parents/:parent_id/resources', function(req, res){
req.flash('on-del-resource', req.originalUrl);
res.render('parents/index')
});
app.get('/resources', function(req, res){
req.flash('on-del-resource', req.originalUrl);
res.render('resources/index')
});
app.get('/resources/:id', function(req, res){
res.render('show');
});
app.delete('/resources/:id', function(req, res){
var redirectDefault = '/resources';
var redirect = req.flash('on-del-resource') || redirectDefault;
DB.delete('resources', id, function(){
if (err) return next(err);
res.redirect(redirect);
});
});
// another option would be to create a middleware which creates methods
req.setRedirection('resource.create'); // set redirect target
req.getRedirection('resource.create'); // get redirect target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment