indexzero
10/25/2011 - 4:49 PM

Declarative JS

Declarative JS

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});';
});
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
        })
      }
  });
});
POST /upload
  parse upload
    with all images
      scale image to 800x600
      save image to disk
      save image to gridfs
[ [ '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' ] ] ] ] ] ] ]
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);
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