123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- var express = require('express');
- var app = express();
- var cors = require('cors');
- var bodyParser = require('body-parser')
- var fs = require('fs');
- app.use(cors());
- app.use(bodyParser.json());
- app.get('/comments', function(req, res){
- fs.readFile('comments.txt', function(err, data){
- return res.status(200).send(JSON.parse(data));
- })
- });
- app.post('/addComment', function(req,res){
- var body = req.body;
- fs.readFile('comments.txt', function(err, data){
- let mainComments = JSON.parse(data);
- mainComments.unshift(body);
- fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
- return res.status(200).send(body);
- })
- })
- });
- app.post('/commentReply', function(req,res){
- var body = req.body;
- fs.readFile('comments.txt', function(err, data){
- let mainComments = JSON.parse(data);
- for (let i = 0; i < mainComments.length; i++) {
- if (mainComments[i].id === body.mainId) {
- mainComments[i].secondComments.unshift(body);
- }
- }
- fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
- return res.status(200).send(body);
- })
- })
- });
- app.delete('/deleteComment/:id', function(req ,res){
- var itemId = +req.params.id
- fs.readFile('comments.txt', function(err, data){
- let mainComments = JSON.parse(data);
- for (let i = 0; i < mainComments.length; i++) {
- if(mainComments[i].id === itemId){
- mainComments.splice(i, 1);
- }
- }
- fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
- return res.status(200).send(data);
- })
- })
-
- });
- app.delete('/deleteSecondComment/:mainId/:secondId', function(req ,res){
- var mainId = +req.params.mainId;
- var secondId = +req.params.secondId;
- fs.readFile('comments.txt', function(err, data){
- let mainComments = JSON.parse(data);
- for (let i = 0; i < mainComments.length; i++) {
- for (let j = 0; j < mainComments[i].secondComments.length; j++) {
- if (mainComments[i].secondComments[j].mainId === mainId) {
- if (mainComments[i].secondComments[j].id === secondId) {
- mainComments[i].secondComments.splice(j,1);
- }
- }
-
- }
- }
- fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
- return res.status(200).send(data);
- })
- })
- });
- app.put('/editComment', function(req ,res){
- var body = req.body
- fs.readFile('comments.txt', function(err, data){
- let mainComments = JSON.parse(data);
- for (let i = 0; i < mainComments.length; i++) {
- if (mainComments[i].id === body.id) {
- mainComments.splice(i,1,body);
- }
- }
- fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
- return res.status(200).send(body);
- })
- })
- });
- app.put('/editSecondComment', function(req ,res){
- var body = req.body
- fs.readFile('comments.txt', function(err, data){
- let mainComments = JSON.parse(data);
- for (let i = 0; i < mainComments.length; i++) {
- for (let j = 0; j < mainComments[i].secondComments.length; j++) {
- if (mainComments[i].secondComments[j].mainId === body.mainId) {
- if (mainComments[i].secondComments[j].id === body.id) {
- mainComments[i].secondComments.splice(j,1,body);
- }
- }
- }
- }
- fs.writeFile('comments.txt',JSON.stringify(mainComments) ,function(err, data){
- return res.status(200).send(body);
- })
- })
- });
- app.post('/img', function(req ,res){
- var body = req.body;
- fs.writeFile('img.jpg',JSON.stringify(body), function(err, data){
- res.status(200).send(body);
- })
-
- })
- app.listen(3001);
|