-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (84 loc) · 2.52 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dotenv/config'
import express from 'express';
const app = express();
import mongoose from 'mongoose';
import documentSchema from './model/documentSchema.js'
import {text} from './text.js'
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({extended: true}));
// Database Connection
mongoose.connect(process.env.MONGO_URL,(err) => {
if(err){
throw err
}
})
//Routes
app.get('/', (req,res) => {
const code = text
res.render('default', { code, lineNumber : code.split("\n").length, page: "> Home" })
});
app.get('/new', (req,res) => {
res.render('new', {page:"> new"});
});
app.post('/save', async (req,res) => {
const code = req.body.value;
try{
const doc = await documentSchema.create({code})
res.redirect(`/${doc._id}`);
} catch {
res.render('new', {value: code});
}
});
app.get("/:id", async(req,res) => {
const id = req.params.id;
try{
const value = await documentSchema.findById(id);
res.render('default', {code : value.code, lineNumber : value.code.split("\n").length, isCode:true, duplicateEdit: true, justText:true, id, page: "> Code"});
} catch {
res.redirect('/new');
}
})
app.get("/:id/duplicateEdit",async(req,res) => {
const id = req.params.id
try{
const value = await documentSchema.findById(id);
res.render('new', {value: value.code, page: "> duplicate & edit"});
} catch {
res.redirect(`/${id}`);
}
});
app.get("/:id/justText", async(req,res) => {
const id = req.params.id
try{
const value = await documentSchema.findById(id);
res.render('jusText', {value: value.code, page: "> just text"});
} catch {
res.redirect(`/${id}`);
}
});
// API for CLI tool
//Upload API
app.post('/saveCLI', async (req,res) => {
const code = req.body.value;
const extension = req.body.extension;
try{
const doc = await documentSchema.create({code, extension})
res.status(200).send(`https://codebinn.herokuapp.com/${doc._id}`);
} catch {
res.status(400).send("Error");
}
});
//Download API
app.post('/downloadCLI', async (req,res) => {
const id = req.body.id;
try{
const value = await documentSchema.findById(id);
res.status(200).send({code : value.code, extension : value.extension});
} catch (err){
res.status(400).send("Error");
}
})
//Port on which the app will listen
const port = process.env.PORT || 5000;
app.listen(port);