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.

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