Here's my CoffeeScript code:
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"
Here's what I'm getting compiled:
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");
As you see, it's superfluous 'cause of plenty returns.
I know about coffeescript-trick with void return on the last line, but after inserting so many void returns, the code becomes larger than compiled.
Are there any ways to generate valid Node.JS code without extra returns?
The whole point is that the returns are there. CoffeeScript is designed to facilitate a functional style of programming, where you probably should be returning something from your functions. Embrace it!
return at the end of the code block where you don't want a return statement to appear.
queryData += data
return