-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (61 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const path = require("path");
const express = require("express");
const app = express();
const fs = require("fs");
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get("/", function (req, res) {
fs.readdir("./tasks", function (err, files) {
res.render("index", { files: files });
});
});
app.get("/tasks/:filename", function (req, res) {
var filename = req.params.filename;
fs.readFile(`./tasks/${filename}`, "utf-8", function (err, filedata) {
filename = filename.replace(".txt", "");
res.render("task", { filename: filename, filedata: filedata });
});
});
app.get("/edit/:filename", function (req, res) {
var filename = req.params.filename;
fs.readFile(`./tasks/${filename}`, "utf-8", function (err, filedata) {
filename = filename.replace(".txt", "");
res.render("edit", { filename: filename, filedata: filedata });
});
});
app.post("/create", function (req, res) {
if (req.body.title == "") {
console.log("Empty file name");
res.redirect("/");
} else {
fs.writeFile(
`./tasks/${req.body.title}.txt`,
req.body.details,
function (err) {
res.redirect("/");
}
);
}
});
app.post("/edit", function (req, res) {
let oldPath = `./tasks/${req.body.title}.txt`;
let newPath = `./tasks/${req.body.updatedTitle}.txt`;
fs.rename(oldPath, newPath, function (err) {
fs.readFile(newPath, "utf-8", function (err, filedata) {
fs.writeFile(newPath, req.body.updatedDetails, function (err) {
console.log("File Updated");
res.redirect("/");
});
});
});
});
app.get("/delete/:filename", function (req, res) {
fs.unlink(`./tasks/${req.params.filename}`, function (err) {
res.redirect('/')
});
});
app.listen(3000, function (req, res) {
console.log("Running on port 3000");
});