-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (47 loc) · 1.71 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
72
73
74
75
76
77
78
79
80
81
const express = require("express");
const path = require("path");
const fs = require("fs");
const { log } = require("console");
const app = express();
const PORT = 3000;
//Set-Up Parsers & EJS
app.set('view engine', 'ejs');
app.use( express.json() );
app.use( express.urlencoded( {extended: true} ));
app.use( express.static( path.join( __dirname, 'public') ));
app.get('/', (req, res) => {
fs.readdir('./files', (err, files) => {
res.render("index", {files: files});
});
});
app.get('/files/:filename', (req, res) => {
fs.readFile(`./files/${req.params.filename}`, "utf-8", (err, filedata) => {
if (err) {
console.error("Error reading file:", err);
return res.status(500).send("Error reading file.");
}
res.render('show', {fileName: req.params.filename, filedata: filedata} );
});
});
app.get('/edit/:filename', (req, res) => {
res.render("edit", {filename: req.params.filename});
});
app.post('/edit', (req, res) => {
const oldPath = path.join(__dirname, 'files', req.body.previous);
const newPath = path.join(__dirname, 'files', req.body.new);
fs.rename(oldPath, newPath, (err) => {
if (err) {
console.error("Error renaming file:", err);
return res.status(500).send("Error renaming file.");
}
res.redirect("/");
});
});
app.post('/create', (req, res) => {
fs.writeFile(`./files/${req.body.title.split(' ').join('')}.txt`, req.body.details, (err) => {
res.redirect('/');
});
});
app.listen(PORT, () => {
console.log("Server Started/Re-Started !!");
});