Skip to content

Instantly share code, notes, and snippets.

View tjanczuk's full-sized avatar

Tomasz Janczuk tjanczuk

View GitHub Profile
@tjanczuk
tjanczuk / gist:1821852
Created February 14, 2012 00:26
Proxied HTTPS request
var http = require('http')
, https = require('https')
res.writeHead(200);
console.log(Object.getOwnPropertyNames(http));
console.log(http)
http.request({ // establishing a tunnel
host: 'itgproxy',
@tjanczuk
tjanczuk / gist:1823175
Created February 14, 2012 03:23
Direct HTTPS request
require('https').get({ host: 'github.com' }, function (bres) {
res.writeHead(bres.statusCode);
bres.pipe(res);
}).on('error', function (err) {
res.writeHead(500);
res.end('Error talking to backend: ' + err);
});
@tjanczuk
tjanczuk / gist:1823438
Created February 14, 2012 03:58
Delayed exception
setTimeout(function () {
throw new Error('an exception');
}, 1000);
@tjanczuk
tjanczuk / gist:1830138
Created February 14, 2012 20:33
Hello world
res.writeHead(200);
res.end('Hello, world!\n');
@tjanczuk
tjanczuk / gist:1830180
Created February 14, 2012 20:38
Console
console.log('Before writeHead');
res.writeHead(200);
console.log('Before write');
res.write('Hello, ');
console.log('Before end');
res.end('world!\n');
console.log('After end');
@tjanczuk
tjanczuk / gist:1831012
Created February 14, 2012 22:18
MongoDB
var query = require('url').parse(req.url, true).query
var mongoUrl = query['db'] || 'mongodb://arr:arr@staff.mongohq.com:10024/arr'
var filter = query['host'] ? { hosts: query['host'] } : {}
require('mongodb').connect(mongoUrl, function (err, db) {
if (notError(err))
db.collection('apps', function (err, apps) {
if (notError(err))
apps.find(filter).toArray(function (err, docs) {
if (notError(err)) {
@tjanczuk
tjanczuk / gist:1831352
Created February 14, 2012 22:51
Proxied HTTP request with 'request'
var request = require('request').defaults({ proxy: 'http://itgproxy:80' })
request('http://www.google.com').pipe(res)
@tjanczuk
tjanczuk / gist:1839682
Created February 15, 2012 22:41
Prevention of persistent global state through cached modules
var http = require('http');
http.Agent.foo = http.Agent.foo || 0;
http.Agent.foo++;
res.writeHead(200);
res.end('foo: ' + http.Agent.foo + '\n');
@tjanczuk
tjanczuk / gist:1848111
Created February 16, 2012 21:45
Hello world
res.writeHead(200);
res.end('Hello, world!\n');
@tjanczuk
tjanczuk / gist:1881128
Created February 22, 2012 03:38
non-strict leaks
//'use strict';
var n = function () {
console.log(new Error().stack)
console.log(arguments)
console.log(arguments.callee)
console.log(arguments.callee.caller)
console.log(Object.getOwnPropertyNames(arguments.callee.caller))
console.log(arguments.callee.caller.prototype)
}