-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (93 loc) · 2.57 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
104
105
106
107
108
const express = require("express");
const app = express()
const {PrismaClient} = require("@prisma/client")
const prisma = new PrismaClient()
const hljs = require('highlight.js');
const MarkdownIt = require('markdown-it')
const emoji = require('markdown-it-emoji');
const md = new MarkdownIt({
linkify: true,
typographer: true,
});
md.use(emoji)
app.use(express.static('public'))
app.set('view engine', 'ejs');
app.use(express.urlencoded())
app.set("views", __dirname + "/views")
app.get("/", (req, res) => {
res.render("index")
})
app.post("/paste", async (req, res) => {
if(!req.body) {
res.status(400).json({"error": "no post data."})
return;
}
const { pasteContent, title, type } = req.body;
let content;
if (type == "code") {
content = hljs.highlightAuto(pasteContent).value
} else if (type == "markdown") {
content = md.render(pasteContent)
} else {
content = pasteContent
}
const paste = await prisma.paste.create({
data: {
content: content,
title: title,
type: type
}
})
res.render("pasted", {url: `https://yussuf-bin.vercel.app/paste/${paste.id}`})
})
app.get("/paste/:id", async (req, res) => {
const { id } = req.params;
try {
const paste = await prisma.paste.findFirstOrThrow({
where: {
id: id
},
include: {
comments: true
}
})
res.render("paste", {
content: paste.content.trim(),
title: paste.title,
type: paste.type,
timestamp: new Date(paste.timestamp).toLocaleDateString(),
comments: paste.comments,
id: id
})
} catch (error) {
//console.log(error)
res.render("404")
}
})
app.post("/paste/:id/comments", async (req, res) => {
const {author, content} = req.body;
if(content.length > 210 || author.length > 50) {
res.send("Message is longer than 210 characters or username is longer than 50 characters!")
return;
}
const pasteId = req.params.id;
const paste = await prisma.paste.findFirstOrThrow({
where: {
id: pasteId
}
})
const comment = await prisma.comment.create({
data: {
body: md.render(content),
name: author,
pasteId: pasteId,
}
})
res.redirect(`/paste/${pasteId}`)
})
app.get("*", (req, res) => {
res.render("404")
})
app.listen(3000, () => {
console.log("http://localhost:3000")
})