index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var express = require('express');
  2. var app = express();
  3. var cors = require('cors');
  4. var bodyParser = require('body-parser')
  5. var fs = require('fs');
  6. app.use(cors());
  7. app.use(bodyParser.json());
  8. app.get('/comments', function(req, res){
  9. fs.readFile('comments.txt', function(err, data){
  10. return res.status(200).send(JSON.parse(data));
  11. })
  12. });
  13. app.post('/addComment', function(req,res){
  14. var body = req.body;
  15. fs.readFile('comments.txt', function(err, data){
  16. let mainComments = JSON.parse(data);
  17. mainComments.unshift(body);
  18. fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
  19. return res.status(200).send(body);
  20. })
  21. })
  22. });
  23. app.post('/commentReply', function(req,res){
  24. var body = req.body;
  25. fs.readFile('comments.txt', function(err, data){
  26. let mainComments = JSON.parse(data);
  27. for (let i = 0; i < mainComments.length; i++) {
  28. if (mainComments[i].id === body.mainId) {
  29. mainComments[i].secondComments.unshift(body);
  30. }
  31. }
  32. fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
  33. return res.status(200).send(body);
  34. })
  35. })
  36. });
  37. app.delete('/deleteComment/:id', function(req ,res){
  38. var itemId = +req.params.id
  39. fs.readFile('comments.txt', function(err, data){
  40. let mainComments = JSON.parse(data);
  41. for (let i = 0; i < mainComments.length; i++) {
  42. if(mainComments[i].id === itemId){
  43. mainComments.splice(i, 1);
  44. }
  45. }
  46. fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
  47. return res.status(200).send(data);
  48. })
  49. })
  50. });
  51. app.delete('/deleteSecondComment/:mainId/:secondId', function(req ,res){
  52. var mainId = +req.params.mainId;
  53. var secondId = +req.params.secondId;
  54. fs.readFile('comments.txt', function(err, data){
  55. let mainComments = JSON.parse(data);
  56. for (let i = 0; i < mainComments.length; i++) {
  57. for (let j = 0; j < mainComments[i].secondComments.length; j++) {
  58. if (mainComments[i].secondComments[j].mainId === mainId) {
  59. if (mainComments[i].secondComments[j].id === secondId) {
  60. mainComments[i].secondComments.splice(j,1);
  61. }
  62. }
  63. }
  64. }
  65. fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
  66. return res.status(200).send(data);
  67. })
  68. })
  69. });
  70. app.put('/editComment', function(req ,res){
  71. var body = req.body
  72. fs.readFile('comments.txt', function(err, data){
  73. let mainComments = JSON.parse(data);
  74. for (let i = 0; i < mainComments.length; i++) {
  75. if (mainComments[i].id === body.id) {
  76. mainComments.splice(i,1,body);
  77. }
  78. }
  79. fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
  80. return res.status(200).send(body);
  81. })
  82. })
  83. });
  84. app.put('/editSecondComment', function(req ,res){
  85. var body = req.body
  86. fs.readFile('comments.txt', function(err, data){
  87. let mainComments = JSON.parse(data);
  88. for (let i = 0; i < mainComments.length; i++) {
  89. for (let j = 0; j < mainComments[i].secondComments.length; j++) {
  90. if (mainComments[i].secondComments[j].mainId === body.mainId) {
  91. if (mainComments[i].secondComments[j].id === body.id) {
  92. mainComments[i].secondComments.splice(j,1,body);
  93. }
  94. }
  95. }
  96. }
  97. fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
  98. return res.status(200).send(body);
  99. })
  100. })
  101. });
  102. app.post('/img', function(req ,res){
  103. var body = req.body;
  104. fs.writeFile('img.jpg',JSON.stringify(body), function(err, data){
  105. res.status(200).send(body);
  106. })
  107. })
  108. app.listen(3001);