Skip to content

Instantly share code, notes, and snippets.

Avatar

Tomasz Janczuk tjanczuk

View GitHub Profile
@tjanczuk
tjanczuk / gist:1821852
Created Feb 14, 2012
Proxied HTTPS request
View gist:1821852
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 Feb 14, 2012
Direct HTTPS request
View gist:1823175
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 Feb 14, 2012
Delayed exception
View gist:1823438
setTimeout(function () {
throw new Error('an exception');
}, 1000);
View gist:1830138
res.writeHead(200);
res.end('Hello, world!\n');
View gist:1830180
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');
View gist:1831012
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 Feb 14, 2012
Proxied HTTP request with 'request'
View gist:1831352
var request = require('request').defaults({ proxy: 'http://itgproxy:80' })
request('http://www.google.com').pipe(res)
@tjanczuk
tjanczuk / gist:1839682
Created Feb 15, 2012
Prevention of persistent global state through cached modules
View gist:1839682
var http = require('http');
http.Agent.foo = http.Agent.foo || 0;
http.Agent.foo++;
res.writeHead(200);
res.end('foo: ' + http.Agent.foo + '\n');
View gist:1848111
res.writeHead(200);
res.end('Hello, world!\n');
@tjanczuk
tjanczuk / gist:1881128
Created Feb 22, 2012
non-strict leaks
View gist:1881128
//'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)
}