【发布时间】:2013-07-20 15:52:18
【问题描述】:
我正在用 express 和 mongo 制作一个 rest api。我没有为每个模型写出 crud 操作,而是考虑编写一个通用的数据库服务,例如(在 db.coffee 中):
exports.findById = (model, req, res) ->
model.findById req.params.id, (err, document)->
if err
throw err
return document
因此,如果我想从用户 ID 中获取用户,我会调用(在 user.coffee 中):
exports.findById = res.send db.findById(model, req, res)
从 app.coffee 调用:
app.get '/user/:id', user.findById(req, res)
你觉得这个设计怎么样?这有标准设计吗?
【问题讨论】: