CoffeeScript – 如何避免不必要的回报?

这是我的CoffeeScript代码:

http.createServer((req, res) -> if req.method is "POST" req.on "data", (data) -> queryData += data req.on "end", -> _.process req.url.substring(1), queryData, (response) -> res.writeHead 200, "Content-Type": "text/plain; charset=utf-8" fs.appendFile "./log", log, (err) -> if err console.log err else res.end response else res.writeHead 405, "Content-Type": "text/plain" res.end() ).listen 55385, "127.0.0.1" 

以下是我正在编译的内容:

 http.createServer(function(req, res) { if (req.method === "POST") { req.on("data", function(data) { return queryData += data; }); return req.on("end", function() { return _.process(req.url.substring(1), queryData, function(response) { res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" }); return fs.appendFile("./log", log, function(err) { if (err) { return console.log(err); } else { return res.end(response); } }); }); }); } else { res.writeHead(405, { "Content-Type": "text/plain" }); return res.end(); } }).listen(55385, "127.0.0.1"); 

如你所见,多余的returns是多余的。

我知道最后一行void void coffeescript-trick,但是插入这么多无效返回后,代码变得比编译的大。

有没有什么办法可以生成有效的Node.JS代码而无需额外的返回?

关键是收益那里。 CoffeeScript被devise为促进编程的function风格,你可能应该从你的函数返回一些东西。 拥抱它!

return代码块的结尾处,您不希望return语句出现。

 queryData += data return