title: how to recover from duplicate key in mongoose + express
body: <p>I've created a signup form using mongoose and express 3</p>
  
  <p>Its possible the user already exists with that username, in which case I get an err.code 11000 (duplicate key). How should I handle existing users?</p>
  
  <p>This is what I'm doing now....but I'm not sure checking error code is best way:</p>
  
  <pre><code>  user.save(function(err){
      if ( err ) {
        console.log(err);
        console.log(err.code);
  
        //duplicate key
        if ( err.code == 11000 ) {
          req.flash('error', 'User already exists');
          res.redirect('/signup');
          return;
        }
      }
  
      res.locals.user = user;
      req.session.user = user;
      //res.locals.session = req.session;
      res.redirect('/');
    });
  </code></pre>
  
  <p>Is there a better way of doing this?</p>