3

At first everything works, and I can successfully store data by posting to the /upload route. But after 120 seconds of inactivity, the timeout event fires, and future attempts to store data fail. However the callback isn't called, so there is no "Unable to insert..." message at the log.

var express = require('express');
var bodyParser = require('body-parser');
var winston = require('winston');
var config = require('./config');
var MongoClient = require('mongodb').MongoClient;

var app = express();

app.use(bodyParser.json());

app.post('/upload', function (req, res) {
  req.json({status: 'recieved'});
  req.app.locals.db.collection('data').insertOne(req.body, function(err, result) {
    if (err === null) {
      winston.info('Successfully inserted', {data: req.body});
    } else {
      winston.warn('Unable to insert', {cause: err});
    }
  });
});

const options = {
  server: {
    socketOptions: {
      keepAlive: 1,
      autoReconnect: true,
      connectTimeoutMS: 5000
    }
  }
};

MongoClient.connect(config.databaseURI, function(err, db) {
  if (err !== null) {
    winston.error('Could not connect to database', {cause: err});
    return;
  }

  db.on('timeout', function (err) {
    winston.error('Mongo timed out', {cause: err});
  });

  app.locals.db = db;
  app.listen(config.port, function() {
    winston.info('Listening on port %d', config.port);
  });
});

I based my code loosely off of this example. It has been suggested to me that I open a new connection to the DB after every request, but this is not best practice since internally, MongoClient.connect is managing a pool. I also tried changing some of the options according to this. Still no luck.

Community
  • 1
  • 1
charmoniumQ
  • 5,214
  • 5
  • 33
  • 51
  • Someone had a similar problem from a while ago [here](http://stackoverflow.com/questions/9670179/application-times-out-when-connecting-to-mongolab-from-heroku). Are you setting the node and npm engine values inside package.json? – Pat Needham Jul 20 '16 at 18:14
  • I tried out the code he has, and heroku refuses to compile the app stating `Unable to download node 0.6.12; does it exist?` Unfortunately the author of that solution recommends downgrading node and npm, but doesn't explain why that fixes anything, so I am not sure which version to try next. – charmoniumQ Jul 25 '16 at 13:34

1 Answers1

3

This solved the problem for me:

var options = { 
  server: { 
    socketOptions: { 
      keepAlive: 300000, connectTimeoutMS: 30000 
    } 
  }, 
  replset: { 
    socketOptions: { 
      keepAlive: 300000, 
      connectTimeoutMS : 30000 
    } 
  } 
};

Then put it in here:

if(process.env.MONGODB_URI) {
  mongoose.connect(process.env.MONGODB_URI, options);
} else {

  // Connect to local database

}
govgo
  • 625
  • 6
  • 18