-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (56 loc) · 1.73 KB
/
server.js
File metadata and controls
67 lines (56 loc) · 1.73 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
const express = require("express")
require('dotenv').config()
const app = express()
app.set("view engine", "ejs")
app.use(express.static("public"))
app.use(express.urlencoded({ extended: true }))
const port = 5000;
const Document = require("./models/Document")
const mongoose = require("mongoose")
const { text } = require("express")
mongoose.connect(process.env.MONGO_DB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
app.get("/", (req, res) => {
const code = `Welcome to Codesniped!
Here you can paste text or code and share it with other , with features like duplication`
res.render("code-display", { code, language: "plaintext", title : "welcome.boi" })
})
app.get("/new", (req, res) => {
res.render("new")
})
app.post("/save", async (req, res) => {
const title = req.body.title
const value = req.body.value
try {
const document = await Document.create({
value : value,
title : title,
})
res.redirect(`/${document.id}`)
} catch (e) {
console.log(e)
res.render("new", { value })
}
})
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 })
} catch (e) {
res.redirect(`/${id}`)
}
})
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 , title : document.title})
} catch (e) {
const code = `Couldn't find the information in Database`
res.render("code-display", { code, language: "plaintext", title : "error.sad" })
}
})
app.listen(process.env.PORT || port, () => console.log(`Visit Me at http://localhost:${port}`));