-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.36 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
fs.readdir(path.join(__dirname, 'files'), (err, files) => {
res.render('index', {
files: files,
});
});
});
app.post('/create', (req, res) => {
fs.writeFile(
`${path.join(__dirname, 'files')}/${req.body.title
.split(' ')
.join('')}.txt`,
req.body.details,
(err) => {
res.redirect('/');
}
);
});
app.get('/files/:filename', (req, res) => {
fs.readFile(
`./files/${req.params.filename}`,
{ encoding: 'utf-8' },
(err, data) => {
res.render('show', {
filename: req.params.filename,
data: data,
});
}
);
});
app.get('/edit/:filename', (req, res) => {
res.render('edit', {
filename: req.params.filename,
});
});
app.post('/edit', (req, res) => {
console.log(req.params);
fs.rename(
`${path.join(__dirname, 'files')}/${req.body.previous}`,
`${path.join(__dirname, 'files')}/${req.body.new}`,
(err) => {
if (err) console.log(err);
res.redirect('/');
}
);
});
app.listen(3000, () => {
console.log(`Running on http://localhost:3000`);
});