Skip to content

Instantly share code, notes, and snippets.

View raycmorgan's full-sized avatar

Ray Morgan raycmorgan

  • Stripe
  • San Francisco, CA
View GitHub Profile
var http = require('http'),
sys = require('sys');
http.createServer(function (request, response) {
response.writeHead(200, {'content-type': 'text/plain'});
response.write("Hello");
response.write(" ");
setTimeout(function () {
// Tested on Node version 0.1.32 and 0.1.33
var http = require('http'),
sys = require('sys');
http.createServer(function (request, response) {
response.writeHead(200, {'content-type': 'text/plain'});
response.write("Hello");
function sanitize(string) {
return string.replace(/[&<>"]/g, escapeReplace);
}
function escapeReplace(char) {
switch (char) {
case '<': return '&lt;';
case '>': return '&gt;';
case '&': return '&amp;';
case '"': return '&quot;';
## Reader interface
event: 'data'
event: 'eof'
method: pause()
method: resume()
## Writer interface
method: write()
method: close()
@raycmorgan
raycmorgan / gist:286071
Created January 25, 2010 17:56
Mu.js -- pre refactor
var Posix = require('posix'),
Path = require('path'),
sys = require('sys');
var Mu = exports;
// capture the base proto
var baseProto = ({}).__proto__;
Mu.cache = {};
Mu.templatePaths = ['.', ''];
@raycmorgan
raycmorgan / Mu.js
Created January 14, 2010 07:58
Mustache compiler in JS for JS
var sys = require('sys');
var template ="<html>" +
"<body>" +
"<h1>{{ title }}</h1>" +
"{{# admin }}" +
"If admin {{ name }}" +
"{{/ admin }}" +
"</body>" +
"</html>";
var p = Promise.join(p1, p2, p3)
p.addCallback(function (p1Res, p2Res, p3Res) {
// p1Res, p2Res, p3Res are arrays
});
// or //
p.addCallback(
function (p1Res) {
var http = require('/http.js');
var sys = require('/sys.js');
var server = http.createServer(function (request, response) {
request.pause();
request.addListener('body', function (chunk) {
sys.puts("Got body: " + chunk);
});
request.addListener('complete', function () {
post('/', function () {
this.parseBody()
.addCallback(function (body) {
});
});
var promise = new node.Promise();
function test_getCookie() {
this.req.header = {Cookie: 'foo=bar; '};
assertMatch(this.request.cookie.foo, "bar");
this.req.header = {};
assertMatch(this.request.cookie.foo, "bar");
this.req.header = {Cookie: 'foo=bar; baz=qux'};
assertMatch(this.request.cookie.foo, "bar");
}