【发布时间】:2012-01-24 19:32:11
【问题描述】:
我正在用 CoffeeScript 编写 Express.js 模块,但我不确定构建它们的最佳方式。
我想使用该模块的方式类似于
app.coffee
Mailer = require('./lib/mailer')
amazon_mailer = new Mailer
key: "somekey"
secret: "somesecret"
type: "SES"
...
amazon_mailer.send(...)
所以,在 Coffeescript 中,我正在考虑这样做:
/lib/mailer.coffee
class Mailer
constructor: (options) ->
@options = options
send: (...) ->
...
module.exports = Mailer
在我的测试中,这是可行的,但这是正确的方法吗?我一直找不到任何关于如何在 CoffeeScript 中构造 express 模块的好例子。有没有更好的方法?
【问题讨论】:
-
在您的示例中,
Mailer只是一个节点模块;它与 Connect/Express 无关。 -
对于它的价值,CoffeeScript 为您的构造函数提供了一些糖。它可以变成:构造函数:(@options) ->
标签: coffeescript express