title: 504 error when inserting into mongo database
body: <p>When using passportjs with nodejs, I get a 504 error when trying to insert into the database.</p>
  
  <p>The mongo database is hosted externally from the application server and the application is behind a reverse proxy (the reverse proxy converts the application port to port 80).</p>
  
  <p>The issue only happens when trying to insert into a mongo document:</p>
  
  <pre><code>passport.use(new GoogleStrategy({
      clientID: "client_id",
      clientSecret: "client_secret",
      callbackURL: "oauth_url_callback"
    },
    function(accessToken, refreshToken, profile, done) {
      passport.serializeUser(function(user,done){
        mongo.connect(url, function(err,db){
          if(err){
            console.log(err, ' was the err');
  
            fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
              fs.writeFileSync('views/debug/debug.txt', err);
            });
  
            done(err);
          } else {
            db.collection('users').find({profile:profile}, function(err, user) {
              if (err) {
                fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
                  fs.writeFileSync('views/debug/debug.txt', err);
                });
  
                db.collection('users').insertOne({profile:profile, name:"John Doe"}, function(err, user){
                  if(err){
                    console.log("failed to insert", err);
                  } else {
                    console.log('Registered a new user!');
                  }
                });
              } else {
                console.log('We have a returning user!');
              }
  
              console.log('testing');
  
              fs.open('views/debug/debug.txt', 'rw', 'a+', function(err,data){
                fs.writeFileSync('views/debug/debug.txt', err);
              });
  
              done(null, user);
            });
          }
        });
      });
    }
  ));
  </code></pre>
  
  <p>I am not sure why this happens, but it only happens when you try to insert into the database. When I refresh the page, it doesn't 504 because it returns a error saying the token has expired.</p>
  
  <p>I also tried logging for errors, and there seems to be no errors. I start the process with both stdout and stderror as arguments to extract output but to no avail.</p>
  
  <p>It also doesn't insert the document/item into the db which is strange.</p>