According to the Meteor tutorials, we should defined our Meteor Methods in both the client and server on account of Latency compensation.
With Meteor Methods and latency compensation, you get the best of both worlds — the security of server code and no round-trip delay.
So I placed my Meteor methods in a ‘Methods’ folder under root, for example:
/Application_Name
/methods
/server
/client
But what about code that has to be run on the server?
For example, the Meteor Email.send(options) call? This is server side only.
I created a function in my server directory /Application_Name/server, just to make a call out to Email.send(), my Meteor.Method in the shared (client/server) /methods calls the function on the server in a Meteor.isServer scoped block:
/methods/email.js
/** Client/Server side stub */
Meteor.methods({
sendMessagesEmail: function (options) {
check(options, {
from: String,
to: String,
subject: String,
html: String
});
this.unblock();
if (Meteor.isServer) {
send_email(options);
}
}
});
/server/email.js
send_email = function (options) {
Email.send(options);
}
I then tried making two Meteor Method calls.
One in the /methods directory, and from there to the /server directory. This can’t be right.
For example:
/methods/email.js
/** Client/Server side stub */
Meteor.methods({
sendMessagesEmail: function (options) {
check(options, {
from: String,
to: String,
subject: String,
html: String
});
this.unblock();
Meteor.call('send_mail', options, function (error) {
if (error) throw new Meteor.error(error);
});
}
});
/server/email.js
Meteor.methods({
send_mail: function (options) {
check(options, {
from: String,
to: String,
subject: String,
html: String
});
this.unblock();
Email.send(options)
}
});
What is the best way to defined Meteor Methods where some code must be on the server?