-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (55 loc) · 1.72 KB
/
Copy pathserver.js
File metadata and controls
71 lines (55 loc) · 1.72 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express');
const mongoose = require('mongoose');
const app = express();
app.use(express.static('public'));
app.use(express.urlencoded({extended: true}))
app.set("view engine" , "ejs");
const uri = process.env.MONGODB_URI;
// setting up the database connection
mongoose.connect(uri, {
useNewUrlParser: true, useUnifiedTopology: true
});
const PORT = process.env.PORT || 8000;
// requiring the model document
const modelDoc = require('./models/modelDoc')
const code = `Welcome to CodeDocShare 👋 !!
The one stop solution to your code / text sharing problems ⭐
Write or Paste snippets of code / text ,save it, edit it and
get a url to share it to world within a fraction of second 🤩 `;
app.get("/" ,(req,res) => {
res.render('displayCode' , { code , language: 'plaintext'})
});
app.get('/new' , (req,res) => {
res.render("newFile");
});
app.post('/save' , async (req,res) => {
const value = req.body.text;
try{
const document = await modelDoc.create({value})
res.redirect(`/${document.id}`);
}catch(err){
res.render("newFile" , { value });
}
});
// the duplicate route
app.get('/:id/duplicate' , async (req,res) => {
const id = req.params.id;
try{
const document = await modelDoc.findById(id)
res.render('newFile' , { value : document.value , id});
}catch(err){
res.redirect('/${id}');
}
});
app.get('/:id' , async (req,res) => {
const id = req.params.id;
try{
const document = await modelDoc.findById(id)
res.render('displayCode' , { code : document.value, id});
}catch(err){
res.redirect(`/`);
}
});
app.listen(PORT , () => {
console.log("Server running on " + PORT);
});