title: Mongoose connection sockets closed
body: <p>I'm using nodejs and MongoDB for the first time and I'm having trouble when I want to connect to the DB.</p>
  
  <p>I read the quickstart from mongoosejs but it still doesn't work, here is my code (right now, all my code is only in one file) :</p>
  
  <pre><code>/***** Utilities *****/
  var express = require('express');
  var app = express();
  var bodyParser = require('body-parser');
  var path = require('path');
  var passport = require('passport');
  var LocalStrategy = require('passport-local').Strategy;
  var mongo = require('mongodb');
  var mongoose = require('mongoose');
  
  app.listen(8080);
  mongoose.connect('mongodb://localhost:8080/users');
  
  var db = mongoose.connection;
  db.on('error', function (error) {
      console.log('/**************** ERROR **************\\');
      console.log(error);
  });
  
  db.on('open', function () {
      console.log("Hello World!");
  });
  
  var Schema = mongoose.Schema;
  var UserSchema = new Schema({
      pseudo: String,
      password: String,
      email: String
  });
  
  var Users = mongoose.model('users', UserSchema);
  
  app.use(bodyParser.urlencoded({
      extended: true
  }));
  app.use(express.static(path.join(__dirname, 'public')));
  app.set('views', __dirname + '/views');
  app.use(passport.initialize());
  app.use(passport.session());
  
  /***** Passport *****/
  passport.serializeUser(function (user, done) {
      done(null, user);
  });
  
  passport.deserializeUser(function (user, done) {
      done(null, user);
  });
  
  /***** Building routes *****/
  app.get('/', function (req, res) {
      res.send('Hello World!');
  });
  
  app.post('/', function (req, res) {
      var pseudo = req.body.pseudo;
      console.log("/******************************\\") console.log(pseudo);
      res.send('Hello ' + pseudo + '!');
  });
  
  app.get('/users', function (req, res) {
      Users.find(function (error, users) {
          if (error) console.log(error);
          else res.render('users.jade', {
              users: users
          })
      });
  })
  
  app.post('/create_user', function (req, res) {
      var user = new Users({
          pseudo: req.body.pseudo,
          password: req.body.password,
          email: req.body.email
      });
      user.save(function (error, user) {
          if (error) console.log(error);
          else res.render('create_user.jade');
      });
  });
  
  app.get('/login', function (req, res) {
      passport.authenticate('users', {
          successRedirect: '/create_user',
          failureRedirect: '/login',
      }) res.render('login.jade
  
      );
  });
  </code></pre>
  
  <p>I'm trying to get data from the login page and store them into one mongo database  but I'm when I start this file, mongo returns the following message:</p>
  
  <p>MongoError: server localhost:8080 sockets closed
  name: 'MongoError',
  message: 'server localhost:8080 sockets closed'</p>
  
  <p>I really don't know how to correct this error.
  Where am I wrong? Is it because of a possible asynchronous problem?</p>