-
-
Save indexzero/1313441 to your computer and use it in GitHub Desktop.
Declarative JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with application | |
set env to production | |
set view engine to jade | |
get /user/tj | |
send "hello" | |
delete /user/tj | |
send "no can do!" | |
listen on port 3000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express") | |
, app = express.createServer(); | |
app.set("env", "production"); | |
app.set("view engine", "jade"); | |
app.get("/user/tj", function(req, res, next){ | |
res.send("hello"); | |
}); | |
app.delete("/user/tj", function(req, res, next){ | |
res.send("no can do!"); | |
}); | |
app.listen(3000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ [ 'rule', 'parse upload' ], | |
[ 'indent' ], | |
[ 'rule', 'with all images' ], | |
[ 'indent' ], | |
[ 'rule', 'scale image to 800x600' ], | |
[ 'rule', 'save image to disk' ], | |
[ 'rule', 'save image to gridfs' ], | |
[ 'outdent' ], | |
[ 'outdent' ], | |
[ 'eos' ] ] | |
[ 'root', | |
[ [ 'rule', 'parse upload' ], | |
[ 'block', | |
[ [ 'rule', 'with all images' ], | |
[ 'block', | |
[ [ 'rule', 'scale image to 800x600' ], | |
[ 'rule', 'save image to disk' ], | |
[ 'rule', 'save image to gridfs' ] ] ] ] ] ] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POST /upload | |
parse upload | |
with all images | |
scale image to 800x600 | |
save image to disk | |
save image to gridfs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/upload', function(req, res){ | |
// parse upload | |
var form = new formidable.IncomingForm; | |
form.keepExtensions = true; | |
form.parse(req, function(err, fields, files) { | |
if (err) return res.send(err); | |
// with all images | |
var vals = files["images"]; | |
for (var i = 0, len = vals.length; i < len; ++i){ | |
var val = vals[i]; | |
var image = val.path; | |
// scale image to 800x600 | |
gm(image) | |
.scale(800, 600) | |
// save image to disk | |
.write(image, "/tmp/whatever", function(err){ | |
// error handling here | |
}); | |
// save image to gridfs | |
grid.putFile(image, "whatever", function(err){ | |
// error handling | |
}) | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compiler.block(/^with all (.*)/, function(body, name){ | |
return 'var vals = files["' + name + '"];\n' | |
+ 'for (var i = 0, len = vals.length; i < len; ++i){\n' | |
+ ' var val = vals[i];\n' | |
+ ' var image = val.path;\n' | |
+ body | |
+ '}'; | |
}); | |
compiler.block(/^(GET|POST|PUT|DELETE) (.*)/, function(body, method, path){ | |
return 'app.' + method.toLowerCase() + '("' + path + '", function(req, res, next){\n' | |
+ body | |
+ '})'; | |
}); | |
compiler.rule(/^scale (.*) to (\d+)x(\d+)/, function(path, w, h){ | |
return 'gm(' + path + ')\n' | |
+ ' .scale(' + w + ', ' + h + ')\n'; | |
}); | |
compiler.rule(/^save (.*) to disk/, function(path){ | |
return '.write(' + path + ', "/tmp/whatever", function(err){\n' | |
+ ' // error handling here'; | |
+ '\n})'; | |
}); | |
compiler.rule(/^save (.*) to gridfs/, function(path){ | |
return 'grid.putFile(' + path + ', "whatever", function(err){\n' | |
+ ' // error handling' | |
+ '\n})'; | |
}); | |
compiler.block(/^parse upload/, function(body){ | |
return 'var form = new formidable.IncomingForm;\n\ | |
form.keepExtensions = true;\n\ | |
form.parse(req, function(err, fields, files) {\n\ | |
if (err) return res.send(err);\n' | |
+ body | |
+ '\n});'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment