123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- "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();
- });
- });
- }
|