Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var addRouter = require('./routes/add');
var deleteRouter = require('./routes/delete');
var completeRouter = require('./routes/complete');
var showRouter = require('./routes/show');
var rewriteRouter = require('./routes/rewrite');

var app = express();

Expand All @@ -23,13 +24,15 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'reactapp/build')));
app.use(express.static(path.join(__dirname, 'data')));
app.use(cors())

app.use('/', indexRouter);
app.use('/add',addRouter);
app.use('/delete',deleteRouter);
app.use('/complete',completeRouter);
app.use('/show',showRouter)
app.use('/show',showRouter);
app.use('/rewrite',rewriteRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
1 change: 0 additions & 1 deletion data/todo_list.json
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
{"todos":[{"value":"7889000","done":false},{"value":"j7y7yh7t6gggh","done":false}]}
3 changes: 0 additions & 3 deletions routes/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var express = require('express');
var fs = require('fs');
var router = express.Router();

/* GET home page. */
router.post('/', function(req, res, next) {
fs.readFile('./data/todo_list.json', {
'encoding': 'utf8'
Expand All @@ -14,8 +13,6 @@ router.post('/', function(req, res, next) {
res.json(true);
});
});

//res.send('hello world')
});

module.exports = router;
17 changes: 17 additions & 0 deletions routes/rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var express = require('express');
var fs = require('fs');
var router = express.Router();

router.post('/', function(req, res, next) {
fs.readFile('./data/todo_list.json', {
'encoding': 'utf8'
}, function (err, data) {
data = JSON.parse(data);
data.todos[req.body.index].value = req.body.text
fs.writeFile('./data/todo_list.json', JSON.stringify(data), function(err) {
res.json(true);
});
});
});

module.exports = router;
39 changes: 21 additions & 18 deletions routes/show.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
var express = require('express');
var fs = require('fs')
var express = require("express");
var fs = require("fs");

var router = express.Router();


/* GET home page. */
router.get('/', function(req, res, next) {
if (fs.existsSync('./data/todo_list.json')) {
fs.readFile('./data/todo_list.json',{
'encoding':'utf-8'
}, function(err,data){
res.json(data.todos)
})
} else {
var data = {
'todos':[]
}
fs.writeFile('./data/todo_list.json',JSON.stringify(data),function(err){
res.json(data)
})
router.get("/", function (req, res, next) {
if (fs.existsSync("./data/todo_list.json")) {
fs.readFile(
"./data/todo_list.json",
{
encoding: "utf-8",
},
function (err, data) {
res.json(data.todos);
}
);
} else {
var data = {
todos: [],
};
fs.writeFile("./data/todo_list.json", JSON.stringify(data), function (err) {
res.json(data);
});
}
});

module.exports = router;
module.exports = router;