-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (32 loc) · 1.18 KB
/
index.js
File metadata and controls
44 lines (32 loc) · 1.18 KB
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
const express = require('express');
const app = express();
const path = require('path');
const fs=require('fs');
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('/',function(req,res){
fs.readdir(`./files`,function(err,files){
res.render("index",{files:files});
})
})
app.get('/file/:filename',function(req,res){
fs.readFile(`./files/${req.params.filename}`,"utf-8",function(err,filedata){ // utf helps to convert data into english
res.render('show',{filename:req.params.filename,filedata:filedata}); // re.rendor means open file in new page
})
})
app.get('/edit/:filename',function(req,res){
res.render('edit',{filename: req.params.filename});
})
app.post('/edit',function(req,res){
fs.rename(`./files/${req.body.previous}`,`./files/${req.body.new}`,function(err){
res.redirect("/");
})
})
app.post('/create',function(req,res){
fs.writeFile(`./files/${req.body.title.split('').join("")}.txt`,req.body.details,function(err){
res.redirect("/");
});
})
app.listen(3000);