server.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. var prepath = process.argv[2] === "local" ? process.cwd()+"/data" : "/app/data";
  3. var http = require("http"),
  4. url = require("url"),
  5. path = require("path"),
  6. fs = require("fs"),
  7. qs = require("querystring"),
  8. port = 3000;
  9. var twit = require("twit");
  10. var config;
  11. try {
  12. config = require(prepath + "/config.js");
  13. } catch (e) {
  14. console.log(prepath + "/config.js not found");
  15. }
  16. var server;
  17. var $lib = require("./bundle/bundle.base");
  18. if (config) {
  19. var Twitter = new twit(config);
  20. server = http.createServer(function(req, res) {
  21. res.end("TEST");
  22. })
  23. } else {
  24. server = http.createServer(experiment1);
  25. }
  26. var stream = Twitter.stream('user', { })
  27. stream.on('message', function (tweet) {
  28. console.log(tweet)
  29. })
  30. server.listen(parseInt(port, 10));
  31. console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
  32. function experiment1(req, res) {
  33. var uri = url.parse(req.url).pathname,
  34. filename = path.join(prepath, uri);
  35. if (req.method === "POST") {
  36. console.log(req.method, req.body)
  37. var body = '';
  38. req.on('data', function(data) {
  39. body += data;
  40. // Too much POST data, kill the connection!
  41. // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
  42. if (body.length > 1e6)
  43. req.connection.destroy();
  44. });
  45. req.on('end', function() {
  46. var post
  47. if (body.substring(0, 1) === "{") {
  48. try {
  49. post = JSON.parse(body);
  50. } catch (e) {
  51. post = {
  52. error: e + ""
  53. }
  54. }
  55. } else {
  56. post = qs.parse(body);
  57. }
  58. // use post['blah'], etc.
  59. res.writeHead(200, {
  60. "Content-Type": "text/plain"
  61. });
  62. function tweetNow(tweetTxt) {
  63. var tweet = {
  64. status: tweetTxt
  65. }
  66. Twitter.post('statuses/update', tweet, function(err, data, response) {
  67. if (err) {
  68. console.log("Error in Replying");
  69. } else {
  70. console.log("Gratitude shown successfully");
  71. }
  72. });
  73. }
  74. tweetNow("Hello World");
  75. res.write(JSON.stringify(post, true, 2) + "\n");
  76. res.end();
  77. });
  78. return;
  79. }
  80. fs.exists(filename, function(exists) {
  81. if (!exists) {
  82. res.writeHead(404, {
  83. "Content-Type": "text/plain"
  84. });
  85. res.write("404 Not Found\n");
  86. res.end();
  87. return;
  88. }
  89. if (fs.statSync(filename).isDirectory()) filename += '/index.html';
  90. fs.readFile(filename, "binary", function(err, file) {
  91. if (err) {
  92. res.writeHead(500, {
  93. "Content-Type": "text/plain"
  94. });
  95. res.write(err + "\n");
  96. res.end();
  97. return;
  98. }
  99. res.writeHead(200);
  100. res.write(file, "binary");
  101. res.end();
  102. });
  103. });
  104. }