title: aws-serverless-express connecting to mongo and get mongo document
body: <p>I am trying to make a Lambda proxy with express using the aws-serverless-express node module. However I keep getting the error 'Error: socket hang up' and I don't know why.</p>
  
  <p>Here is my lambda code:</p>
  
  <pre><code> const app = require('./app');
   const awsServerlessExpress = require('aws-serverless-express');
   const server = awsServerlessExpress.createServer(app);
  
   exports.handler = (event, context, callback) =&gt; {
    console.log('EVENT: ' + JSON.stringify(event));
    awsServerlessExpress.proxy(server, event, context);
  }
  </code></pre>
  
  <p>and here is my express code: </p>
  
  <pre><code>MongoClient.connect("mongodb:/mongoUriGoesHere", function(err, db) {
  if(err) { return console.dir(err); }
  
  let collection = db.collection('collection');
  
  app.get('/hello', (req, res) =&gt; {
   collection.find().toArray()
    .then((documents) =&gt; /* Handle success (console.log) */)
    .catch((err) =&gt; /* Handle error */)
   })
  });
  
  module.exports = app;
  </code></pre>
  
  <p>This is my response from the lambda</p>
  
  <p><a href="https://i.stack.imgur.com/Y0ybB.png" rel="nofollow noreferrer">https://i.stack.imgur.com/Y0ybB.png</a></p>
  
  <p>I know lambda functions are supposed to be stateless when creating a connection to a db, but I don't know how to get this to work. I've also tried using mongoose and no luck. I can get it to work when there is no code that attemps to connect to the db, but this is my problem at the moment. Any answers would be greatly appreaciated. Been at this for a while and still can't figure it out!</p>