title: node.js express mongodb save messages?
body: <p>I'm trying to see express, mongoose and MongoDB work together, in a really simple way. If I can have a box, put my name in there, submit it and have it save to the DB, that's all I need.</p>
  <p>I tried doing this from taking pieces of tutorials but I'm stuck. I was trying to do it through a chat and just save every message. If you can get my example to work or have one of your own, either way, It just helps me see something that works.</p>
  <p>Then I can have a foundation of what works and can add to it. I see a lot of tutorials and everything else, but not something simple for people, it's always a big project you get lost on somewhere, hopefully, this will be helpful to others too, it's a lot, with everything, with node and all its friends.</p>
  <p>With my example, I am not getting any errors, that I see, but there may be some. And I'm looking in DB chat to see the messages using db.messages.find() but there not in there.</p>
  <p>HTML:</p>
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Chat with socket.io and node.js&lt;/title&gt;
          &lt;style&gt;
              #chat{
                  height:500px;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id=&quot;chat&quot;&gt;&lt;/div&gt;
          &lt;form id=&quot;send-message&quot;&gt;
              &lt;input size=&quot;35&quot; id=&quot;message&quot;&gt;&lt;/input&gt;
              &lt;input type=&quot;submit&quot;&gt;&lt;/input&gt;
          &lt;/form&gt;
  
          &lt;script src=&quot;http://code.jquery.com/jquery-latest.min.js&quot;&gt;&lt;/script&gt;
          &lt;script src=&quot;/socket.io/socket.io.js&quot;&gt;&lt;/script&gt;
          &lt;script&gt;
              jQuery(function($){
                  var socket = io.connect();
                  var $messageForm = $('#send-message');
                  var $messageBox = $('#message');
                  var $chat = $('#chat');
         
                  $messageForm.submit(function(e){
                      e.preventDefault();
                      socket.emit('send message', $messageBox.val());
                      $messageBox.val('');
                  });
         
                  socket.on('new message', function(data){
                      $chat.append(data + &quot;&lt;br/&gt;&quot;);
                  });
              });
  
              var chatSchema = mongoose.Schema({
                  msg: String,
                  created: {type: Date, default: Date.now}
              });
  
              var Chat = mongoose.model('Message', chatSchema);
  
              var newMsg = new Chat({msg: msg});
              newMsg.save(function(err){
                  if(err) throw err;
              });               
  
  
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Server.js</p>
  <pre><code>var mongoose = require('mongoose')
  var express = require('express'),
  app = express(),
  server = require('http').createServer(app),
  io = require('socket.io').listen(server);
  
  server.listen(3000);
  
  app.get('/', function(req, res){
      res.sendfile(__dirname + '/index.html');
  });
  
  io.sockets.on('connection', function(socket){
      socket.on('send message', function(data){
          io.sockets.emit('new message', data);
      });
  });
  
  mongoose.connect('mongodb://localhost/chat', function(err){
      if(err){
          console.log(err);
      } else{
          console.log('Connected to mongodb!');
      }
  });
  </code></pre>