server.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. function rr(name) {
  2. console.log("RRR:", name)
  3. return require(name);
  4. }
  5. var util = require("util");
  6. var config = {};
  7. try {
  8. //running in docker / cloudron
  9. util._extend(config, require("/app/data/config.json"));
  10. }catch(e){
  11. console.log("no config present")
  12. }
  13. console.log("CONFIG",config);
  14. var express = require("express");
  15. var app = require('express')();
  16. var myserver = require('http').Server(app);
  17. var io = require('socket.io')(myserver);
  18. var chat = io
  19. .of('/chat')
  20. .on('connection', function (socket) {
  21. socket.emit('a message', {
  22. that: 'only'
  23. , '/chat': 'will get'
  24. });
  25. chat.emit('a message', {
  26. everyone: 'in'
  27. , '/chat': 'will get'
  28. });
  29. });
  30. var news = io
  31. .of('/news')
  32. .on('connection', function (socket) {
  33. console.log("KLLK");
  34. socket.on("woot",function(){
  35. console.log("SSSSS",arguments);
  36. })
  37. socket.emit('item', { news: 'item' });
  38. });
  39. if(config.peers){
  40. setTimeout(function(){
  41. var lives = config.peers.map(function(peer){
  42. var con = require("socket.io-client")('http://'+peer.ip+':'+peer.port+"/chat");
  43. con.on('connect', function () {
  44. chat.emit('hi!');
  45. console.log("CHATE",arguments);
  46. }).on("a message",function(message){
  47. console.log("cccARG",message);
  48. });
  49. return {peer:peer,con: con }
  50. })
  51. console.log(lives)
  52. },5000)
  53. /*
  54. other_server.on("connect",function(){
  55. other_server.on('message',function(data){
  56. // We received a message from Server 2
  57. // We are going to forward/broadcast that message to the "Lobby" room
  58. console.log("LOG GOT MESSAGE");
  59. chat.emit("a message",data);
  60. });
  61. });
  62. */
  63. }
  64. function tester(){
  65. function newsa(){
  66. news.emit("item",{"title":"titlllklkl","date":new Date(),"body":"jljlkjl"})
  67. setTimeout(newsa,Math.floor(Math.random()*100)*1000)
  68. }
  69. function chata(){
  70. chat.emit("a message",{"user":"u"+Math.random(),"date":new Date(),"body":"jljlkjl"})
  71. setTimeout(chata,Math.floor(Math.random()*20)*1000)
  72. }
  73. setTimeout(newsa,Math.floor(Math.random()*100)*1000)
  74. setTimeout(chata,Math.floor(Math.random()*20)*1000)
  75. }
  76. tester();
  77. app.use(express.static("/app/data"));
  78. app.get("/", function(req, res, next) {
  79. try {
  80. var a = require("." + req.url)(req, res, next);
  81. } catch (e) {
  82. res.send(e);
  83. }
  84. })
  85. var http = require('http'),
  86. formidable = require('formidable'),
  87. fs = require('fs'),
  88. path = require('path');
  89. // All your express server code goes here.
  90. // ...
  91. // Upload route.
  92. app.post('/upload', function(req, res) {
  93. var form = new formidable.IncomingForm();
  94. form.multiples = true;
  95. form.parse(req, function(err, fields, afiles) {
  96. // `file` is the name of the <input> field of type `file`
  97. console.log(afiles, fields)
  98. var ffa = afiles.file[0];
  99. var old_path = ffa.path,
  100. file_size = ffa.size,
  101. file_ext = ffa.name.split('.').pop(),
  102. index = old_path.lastIndexOf('/') + 1,
  103. file_name = old_path.substr(index),
  104. new_path = path.join("/app/data", '/uploads/', file_name + '.' + file_ext);
  105. fs.readFile(old_path, function(err, data) {
  106. fs.writeFile(new_path, data, function(err) {
  107. fs.unlink(old_path, function(err) {
  108. if (err) {
  109. res.status(500);
  110. res.json({
  111. 'success': false
  112. });
  113. } else {
  114. res.status(200);
  115. res.json({
  116. 'success': true
  117. });
  118. }
  119. });
  120. });
  121. });
  122. });
  123. });
  124. myserver.listen(3000);