0

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.

4

1 回答 1

1

您的 EmailProvider 变量是对函数的引用,而不是对象实例。这可能会导致两个问题:

  1. thisEmailProvider.js 上的运算符可能不会像您希望的那样引用 EmailProvider。
  2. 每次调用EmailProvider.save()都会db.collection再次运行,这可能会导致性能问题、内存泄漏以及其他问题。

您应该从 EmailProvider 创建一个对象实例,如下所示:

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

如果this操作员导致问题(例如它无法识别 EmailProvider 方法),请在call函数之后调用save函数,如下所示:

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

希望它会有所帮助。

于 2013-05-20T22:17:22.090 回答