How do I make a simple form GET and POST using Express and Mongo?

I'm trying to follow this Express.js and MongoDB tutorial with the difference of making my index page have a form where you save an email address.

This is the main code so far:

emailprovider.js

var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;

EmailProvider = function(host, port) {
  this.db= new Db('email-test', new Server(host, port, {safe: false}, {auto_reconnect: true}, {}));
  this.db.open(function(){});
};


EmailProvider.prototype.getCollection= function(callback) {
  this.db.collection('emails', function(error, email_collection) {
    if( error ) callback(error);
    else callback(null, email_collection);
  });
};

//save new email
EmailProvider.prototype.save = function(emails, callback) {
    this.getCollection(function(error, email_collection) {
      if( error ) callback(error)
      else {
        if( typeof(emails.address)=="undefined")
          emails = [emails];

        for( var i =0;i< emails.address;i++ ) {
          email = emails[i];
          email.created_at = new Date();
        }

        email_collection.insert(emails, function() {
          callback(null, emails);
        });
      }
    });
};

exports.EmailProvider = EmailProvider;

app.js

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , EmailProvider = require('./emailprovider').EmailProvider;

var app = express();

app.get('/', routes.index);
app.get('/users', user.list);


//Routes


//get new email form
app.get('/email/new', function(req, res) {
    res.render('email_new', {
        title: 'Email Address'
    });
});


//save new email
app.post('/email/new', function(req, res){
    emailProvider.save({
        address: req.param('name')
    }, function( error, docs) {
        res.redirect('/')
    });
});

index.js

  .form-area
    form(method='post')
     input(type='text', name='address', placeholder='Your email address')
     input(type='submit', value='Send')  

I really don't have a clue what's going on in the emailprovider. I kinda got the routes down but right now when I try to save a new Email I get a Cannot POST / 404 error message. What's the correct way to do this?

Thanks.

EDIT: removed extra commas in jade syntax for input attributes.

Your EmailProvider variable is a reference to function and not an object instance. This may cause two problems:

  1. The this operator on EmailProvider.js might not refer to EmailProvider as you wish.
  2. Every call to EmailProvider.save() will run the db.collection again which can cause performance issues, memory leak as well other issues.

You should create an object instance from EmailProvider as follow:

var app = express();
var emailProvider = new EmailProvider(<some host>, <some port>);
...

If this operator causes problem (such as it does not recognize EmailProvider methods) call the call function after the save function as follow:

//save new email
app.post('/email/new', function(req, res){
  emailProvider.save({
      address: req.param('name')
  }, function( error, docs) {
      res.redirect('/')
  }).call(emailProvider);
});

Hope it will help.