You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.8 KiB

  1. var http = require('http');
  2. const fs = require('fs');
  3. var url = require('url');
  4. var path = require('path');
  5. var StringDecoder = require('string_decoder').StringDecoder;
  6. var exec = require("child_process").exec;
  7. var Promise= require('promise');
  8. var mimeTypes = {
  9. "html": "text/html",
  10. "jpeg": "image/jpeg",
  11. "jpg": "image/jpeg",
  12. "png": "image/png",
  13. "js": "text/javascript",
  14. "css": "text/css",
  15. "json": "text/plain"};
  16. function writenodes(nodes) {
  17. fs.writeFile('public/Verbesserungen.txt', nodes, { flag: 'w' }, (err) => {
  18. if (err)
  19. console.log(err);
  20. else {
  21. console.log("File written successfully\n");
  22. console.log("The written has the following contents:");
  23. console.log(fs.readFileSync("public/nodes.txt", "utf8"));
  24. }
  25. })};
  26. // function writeedges(edges) {
  27. //
  28. // fs.writeFile('public/edges.txt', edges, { flag: 'w' }, (err) => {
  29. // if (err)
  30. // console.log(err);
  31. // else {
  32. // console.log("File written successfully\n");
  33. // console.log("The written has the following contents:");
  34. // console.log(fs.readFileSync("public/edges.txt", "utf8"));
  35. // }
  36. // })};
  37. // the following read funcitons are not asyncronous, that means they will block the process..
  38. // for scalability this has to be changed in future
  39. // function readnodes() {
  40. // var input = fs.readFileSync('public/nodes.txt', 'utf8')
  41. // // console.log('input ooohyeah',input)
  42. // // // var danodes = JSON.parse(input);
  43. // //
  44. // // const lines = input.split(/\r?\n/);
  45. // // console.log('line numbaaa 1', lines, lines[1], lines[1].length)
  46. // // var string = '';
  47. // // var i;
  48. // //
  49. // // for (i = 0; i < (lines.length - 2); i++) {
  50. // // string += '"data' + i.toString() + '":' + lines[i] + ' ,' ;
  51. // // };
  52. // // string += '"data' + lines.length.toString() + '":' + lines[lines.length - 2] ;
  53. // // string = '{' + string + '}' ;
  54. // // const jsonlines = JSON.parse(string)
  55. // //
  56. // //
  57. // // let sortedlines = [];
  58. // // sortedlines = [].slice.call(jsonlines).sort(function(a,b) {
  59. // // var x = a.length;
  60. // // var y = b.length;
  61. // // if (x < y) {
  62. // // return -1;
  63. // // }
  64. // // if (x > y) {
  65. // // return 1;
  66. // // }
  67. // // return 0;
  68. // //
  69. // // });
  70. // // console.log('daline1 in nodes', sortedlines)
  71. // return input
  72. // };
  73. // function readedges() {
  74. //
  75. // var daedges = fs.readFileSync('public/edges.txt', 'utf8');
  76. // // const lines = daedges.split(/\r?\n/);
  77. // // // console.log(lines)
  78. // // sortedlines = lines.sort(function(a,b) {
  79. // // var x = a.length;
  80. // // var y = b.length;
  81. // // if (x < y) {
  82. // // return -1;
  83. // // }
  84. // // if (x > y) {
  85. // // return 1;
  86. // // }
  87. // // return 0;
  88. // //
  89. // // });
  90. // // console.log('daline1 in edges', sortedlines[0])
  91. // return daedges
  92. // };
  93. //
  94. http.createServer((request, response)=>{
  95. var pathname = url.parse(request.url).pathname;
  96. var filename;
  97. if(pathname === "/"){
  98. filename = "Prototyp.html";
  99. }
  100. else{
  101. filename = path.join(process.cwd(), pathname);
  102. }
  103. try{
  104. fs.accessSync(filename, fs.F_OK);
  105. var fileStream = fs.createReadStream(filename);
  106. var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
  107. response.writeHead(200, {'Content-Type':mimeType});
  108. fileStream.pipe(response);
  109. if (mimeType === "text/plain"){
  110. // Get the payload,if any
  111. var decoder = new StringDecoder('utf-8');
  112. var buffer = '';
  113. var theanswer = '';
  114. request.on('data', function(data) {
  115. buffer += decoder.write(data);
  116. });
  117. var theanswer = '';
  118. console.log('i am at least come til here')
  119. request.on('end', function() {
  120. buffer += decoder.end();
  121. if (buffer[3] === 'i'){
  122. console.log(buffer);
  123. }
  124. if (buffer[3] === 'f'){
  125. console.log(buffer);
  126. theanswer = 'your changes were taken into the\n\nM A T R I X'
  127. }
  128. console.log(buffer[3])
  129. if (buffer[3] === 'e'){
  130. // console.log('oleola',nodes)
  131. }
  132. const execpython = new Promise((resolve, reject) => {
  133. exec("python script.py > public/data.json", (error, stdout, stderr) => {
  134. if (error) {
  135. console.log(`error: ${error.message}`);
  136. reject(error)
  137. } else {
  138. console.log(`stdout: ${stdout}`);
  139. resolve(stdout)
  140. }
  141. });
  142. })
  143. const checkIfItsDone = () => {
  144. execpython
  145. .then(ok => {
  146. console.log(ok)
  147. })
  148. .catch(err => {
  149. console.log(err)
  150. })
  151. }
  152. checkIfItsDone()
  153. // var string = 'oi';
  154. // // var string = execpython('oi');
  155. // // console.log('output of execpython', string);
  156. // Promise.resolve(string)
  157. // .then(execpython(string))
  158. // .then(s => response.end(s))
  159. // Log the request/response
  160. console.log('Payload received: ',buffer);
  161. });
  162. }
  163. }
  164. catch(e) {
  165. console.log('File not exists: ' + filename);
  166. response.writeHead(404, {'Content-Type': 'text/plain'});
  167. response.write('404 Not Found\n');
  168. response.end();
  169. return;
  170. }
  171. return;
  172. }
  173. ).listen(5000);