title: Error connecting to mongolab using node.js and mongoose
body: <p>I'm trying to write an API using mongoose and mongolab.</p>
  
  <p>Here is my code :</p>
  
  <p>./index.js</p>
  
  <pre><code>var express    = require('express');        
  var app        = express();                 
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  
  mongoose.connect('mongodb://user:pass@****.mlab.com:*****/****');
  
  var Bear = require('./app/models/bear');
  
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  
  
  
  var port = process.env.PORT || 8080;
  
  var router = express.Router();
  
  router.use(function(req, res, next){
      console.log('request called');
      next();
  });
  
  router.get('/', function(req, res) {
      res.json({ message: 'in /' });   
  });
  
  router.route('/bears')
  
      .post(function(req, res) {
  
          var bear = new Bear();      
          bear.name = req.body.name;
          console.log(bear.name);
          bear.save(function(err) {
              console.log('in save')
              if (err)
                  res.send(err);
  
              res.json({ message: 'Bear created!' });
          });
  
      });
  
  app.use('/api', router);
  
  app.listen(port);
  console.log('connected to port ' + port);
  </code></pre>
  
  <p>and </p>
  
  <p>./app/models/bear.js</p>
  
  <pre><code>var mongoose     = require('mongoose');
  var Schema       = mongoose.Schema;
  
  var BearSchema   = new Schema({
      name: String
  });
  
  module.exports = mongoose.model('Bear', BearSchema);
  </code></pre>
  
  <p>My problem is, when I try the following request : </p>
  
  <p><a href="https://i.stack.imgur.com/vjqzu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vjqzu.png" alt="postman"></a></p>
  
  <p>It doesn't return anything. I tried to console.log something in bear.save but in never goes in.</p>
  
  <p>I don't get what I am doing wrong. Also when I try to connect to my mongolab db from my terminal it works perfectly well.</p>