"use strict"; var prepath = process.argv[2] === "local" ? process.cwd()+"/data" : "/app/data"; var http = require("http"), url = require("url"), path = require("path"), fs = require("fs"), qs = require("querystring"), port = 3000; var twit = require("twit"); var config; try { config = require(prepath + "/config.js"); } catch (e) { console.log(prepath + "/config.js not found"); } var server; var $lib = require("./bundle/bundle.base"); if (config) { var Twitter = new twit(config); server = http.createServer(function(req, res) { res.end("TEST"); }) } else { server = http.createServer(experiment1); } var stream = Twitter.stream('user', { }) stream.on('message', function (tweet) { console.log(tweet) }) server.listen(parseInt(port, 10)); console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown"); function experiment1(req, res) { var uri = url.parse(req.url).pathname, filename = path.join(prepath, uri); if (req.method === "POST") { console.log(req.method, req.body) var body = ''; req.on('data', function(data) { body += data; // Too much POST data, kill the connection! // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB if (body.length > 1e6) req.connection.destroy(); }); req.on('end', function() { var post if (body.substring(0, 1) === "{") { try { post = JSON.parse(body); } catch (e) { post = { error: e + "" } } } else { post = qs.parse(body); } // use post['blah'], etc. res.writeHead(200, { "Content-Type": "text/plain" }); function tweetNow(tweetTxt) { var tweet = { status: tweetTxt } Twitter.post('statuses/update', tweet, function(err, data, response) { if (err) { console.log("Error in Replying"); } else { console.log("Gratitude shown successfully"); } }); } tweetNow("Hello World"); res.write(JSON.stringify(post, true, 2) + "\n"); res.end(); }); return; } fs.exists(filename, function(exists) { if (!exists) { res.writeHead(404, { "Content-Type": "text/plain" }); res.write("404 Not Found\n"); res.end(); return; } if (fs.statSync(filename).isDirectory()) filename += '/index.html'; fs.readFile(filename, "binary", function(err, file) { if (err) { res.writeHead(500, { "Content-Type": "text/plain" }); res.write(err + "\n"); res.end(); return; } res.writeHead(200); res.write(file, "binary"); res.end(); }); }); }