【发布时间】:2015-05-18 04:06:24
【问题描述】:
我目前正在开发一个使用 Express.js 和 Mongoose 的 node.js Web 服务。最近,我想我会尝试使用 CoffeeScript,因为我听说有一些好处。但是,我注意到一些令人不安的事情,我想知道是否有人可以澄清一下。
这是我用纯 javascript 编写的路线之一:
router.post('/get/:id', decorators.isManager, function(req, res) {
Group.findById(req.params.id, function(err, grp) {
if(err) res.status(500).end();
if(grp == null) res.status(500).send('The group could not be found');
res.send(grp);
});
});
但是,使用以下(几乎等效的咖啡脚本):
router.post '/get/:id', decorators.isManager, (req, res) ->
Group.findById req.params.id, (err, grp) ->
res.status(500).end() if err
res.status(500).send 'The group could not be found' if not grp?
res.send grp
将其重新编译为 javascript 会返回以下内容:
router.post('/get/:id', decorators.isManager, function(req, res) {
return Group.findById(req.params.id, function(err, grp) {
if(err) res.status(500).end();
if(grp == null) res.status(500).send('The group could not be found');
return res.send(grp);
});
});
这些额外的回报会影响我的应用程序的性能,它会改变它的工作方式吗?我对其进行了测试,它似乎是相同的响应时间,但是我不确定这会对多个嵌套查询产生什么影响。谢谢!
【问题讨论】:
-
如果您想明确返回任何内容,只需在 CoffeeScript 函数的末尾添加
return。 -
这是 Ruby 20 年来一直在做的事情。没关系。隐式回报很棒,您不必担心。
标签: javascript node.js express coffeescript