-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (55 loc) · 1.5 KB
/
server.js
File metadata and controls
57 lines (55 loc) · 1.5 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
45
46
47
48
49
50
51
52
53
54
55
56
57
const express=require('express')
const app=express();
const mongoose=require('mongoose');
require('dotenv').config()
const Document=require('./models/Document');
app.set('view engine','ejs');
app.use(express.static('public'));
app.use(express.urlencoded({extended:true}))
mongoose.connect(`mongodb+srv://shubham_user:${process.env.DATABASE_PASSWORD}@cluster0.xxeot.mongodb.net/my_database?retryWrites=true&w=majority`,
{useUnifiedTopology:true,useNewUrlParser:true})
app.get('/',(req,res)=>{
const code=`Welcome to Wastebin
Use the commands in the top right corner
to create a new file to share with others`
res.render('code-display',{code,language:'plaintext'});
})
app.get('/new',(req,res)=>{
res.render("new",{canSave:true})
})
app.post('/save',async (req,res)=>{
const value=req.body.value;
try{
const document=await Document.create({value});
res.redirect(`/${document.id}`)
}
catch(e)
{ console.log(e);
res.render("new",{value});
}
})
app.get('/:id',async (req,res)=>{
const id=req.params.id;
try{
const document=await Document.findById(id);
res.render('code-display',{code:document.value,id})
}
catch(e)
{
res.redirect('/');
}
})
app.get('/:id/duplicate',async (req,res)=>{
const id=req.params.id;
try{
const document=await Document.findById(id);
res.render('new',{value:document.value,canSave:true})
}
catch(e)
{
res.redirect(`/${id}`);
}
})
app.listen(3001,()=>{
console.log("running");
});