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
| // this is an example of what the first pass at native (fab) templates will look like: | |
| var fab = require("fab") | |
| , listen = fab.listen | |
| ( fab ) | |
| ( listen, 0xFAB ) | |
| ( /\// ) |
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
| // this is an example of what the first pass at native (fab) templates will look like: | |
| with ( require( "fab" ) ) fab | |
| ( listen, 0xFAB ) | |
| ( /\// ) | |
| ( /1/ ) | |
| ( "This is a template with no variables: a unary app" ) |
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
| // no global leakage this way | |
| function createNamedFunction(name) { | |
| var args = Array.prototype.slice.call(arguments, 1); | |
| var code = args.pop(); | |
| return process.compile("(function "+name+"("+args.join(",")+"){"+body+"\n})" | |
| , "createNamedFunction-"+name); | |
| } |
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
| -- I used to do it like this: | |
| SELECT * FROM table WHERE | |
| hurr | |
| AND (foo = 'bar') | |
| AND (bar = 'foo') | |
| -- Then later I spaced it out even more: | |
| SELECT |
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 sys = require('sys'); | |
| var url= require('url'); | |
| var query = url.parse('http://www.freddymac.com?name=freddy&lastname=mac', true).query; | |
| sys.puts(query.name); // -> freddy | |
| // or use querystring directly: | |
| var querystring = require("querystring"); | |
| var query = querystring.parse("name=joe&lastname=schmoe"); | |
| sys.puts(query.name); // -> joe |
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
| // This returns a Javascript string | |
| require.registerExtension('.coffee', function(async, source, callback) { | |
| return compileCoffee(source); | |
| }); | |
| // True | |
| require('coffee').test === 123; | |
| // This returns a object | |
| require.registerExtension('.mu', function(async, source, callback) { |
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
| function( request, response ) { | |
| /* Determine Encoding */ | |
| var isGzip = /gzip/.test( request.headers['accept-encoding'] ) ? true : false; | |
| /* Extract Scope */ | |
| var sandbox = /\.+(.+)\.sb+/.exec( request.headers.host ); | |
| if ( sandbox !== null ) { | |
| var scope = sandbox[1]; |
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 sys = require("sys"), sax = require("../sax-js/lib/sax"); | |
| parser = sax.parser(false); | |
| sax.EVENTS.forEach(function (ev) { | |
| parser["on" + ev] = function (n) { sys.puts(ev + " " + sys.inspect(n)); }; | |
| }); | |
| parser.write("<span>Welcome,</span> to monkey land").close(); | |
| /* | |
| $ node sax-test.js | |
| opentag { |
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
| Get(/^\/myroute/, function(request) { | |
| ... | |
| }); | |
| Post(/^\/myroute/, function(request) { | |
| ... | |
| }); | |
| Head(/^\/myroute/, function(request) { | |
| ... |
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
| // EXAMPLE 1: async response | |
| // PSGI equivalent | |
| var app = function(env) { | |
| return function(respond) { | |
| // do some event stuff | |
| setTimeout(function() { | |
| respond({ status : code, headers : headers, body : body }); | |
| }, 1000); | |
| } |