I’m writing Express.js modules in CoffeeScript, and I’m not sure of the best way to structure them.
The way I want to utilize the module is something like
app.coffee
Mailer = require('./lib/mailer')
amazon_mailer = new Mailer
key: "somekey"
secret: "somesecret"
type: "SES"
...
amazon_mailer.send(...)
So, in Coffeescript, I’m thinking about doing it this way:
/lib/mailer.coffee
class Mailer
constructor: (options) ->
@options = options
send: (...) ->
...
module.exports = Mailer
In my testing, this works, but is it the proper way to do it? I’ve been unable to find any good examples about how to structure express modules in CoffeeScript. Is there a better way to do it?
Yes, your approach is fine. It’s common to export a constructor from a Node library.
The only thing you have to worry about is exporting the
Mailerclass in such a way that it can berequired directly. You can do that by adding the lineafter defining the class.