server.js (1332B)
1 var httpProxy = require('./vendor/http-proxy/node-http-proxy'), 2 querystring = require('querystring'); 3 4 var handleTweet = function (req, res, proxy) { 5 var input = ''; 6 req.setEncoding('utf8'); 7 req.on('data', function (chunk) { input += chunk; }); 8 req.once('end', function () { 9 var tweet = querystring.parse(input), 10 blacklist = ['lat', 'long', 'place_id', 'display_coordinates']; 11 for (var i = 0; i < blacklist.length; i++) { 12 // if tweet has blacklisted property, reject 13 if (tweet.hasOwnProperty(blacklist[i])) { 14 console.log('bad tweet: ' + blacklist[i]); 15 res.writeHead(400); 16 res.write('Bad Request'); 17 return res.end(); 18 } 19 } 20 var options = { 21 host: 'api.twitter.com', 22 port: 80, 23 buffer: { 24 resume: function () { 25 req.emit('data', input); 26 req.emit('end'); 27 } 28 } 29 }; 30 process.nextTick(function () { 31 proxy.proxyRequest(req, res, options); 32 }); 33 }); 34 }; 35 36 var handleRest = function (req, res, proxy) { 37 proxy.proxyRequest(req, res, { host: 'api.twitter.com', post: 80 }); 38 }; 39 40 httpProxy.createServer(function (req, res, proxy) { 41 if (req.method == 'POST' && req.url == '/statuses/update.json') { 42 return handleTweet(req, res, proxy); 43 } 44 handleRest(req, res, proxy); 45 }).listen(80);