-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (61 loc) · 1.82 KB
/
server.js
File metadata and controls
73 lines (61 loc) · 1.82 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
72
73
const express = require("express");
const app = express();
require("dotenv").config();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(express.urlencoded({ extended: true }));
const mongoose = require("mongoose");
const Document = require("./models/Document");
// const URI = `mongodb+srv://admin:${process.env.PASSWORD}@dip-bin.ysmvr.mongodb.net/Randomtext?retryWrites=true&w=majority`;
const URI = process.env.MONGODB_URI;
app.listen(process.env.PORT);
(async () => {
try {
const connection = await mongoose.connect(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (e) {
console.log(e);
}
})();
app.get("/", (req, res) => {
const code = `Sharing code is a good thing, and it should be _really_ easy to do it.
A lot of times, I want to show you something I'm seeing - and that's where we
use pastebins.Haste is the prettiest, easiest to use pastebin ever made`;
res.render("code_window", { code, language: "plaintext" });
});
app.get("/new", (req, res) => {
res.render("new");
});
app.post("/save", async (req, res) => {
const value = req.body.value;
try {
//create() is a static function of the Model class
const document = await Document.create({ text: value });
const id = document._id.toString();
res.redirect(`/${id}`);
// console.log(document._id.toString());
} catch (e) {
console.log(e);
res.render("new", { value });
}
});
app.get("/:id", async (req, res) => {
const id = req.params.id;
try {
const doc = await Document.findById(id);
res.render("code_window", { code: doc.text, id });
} catch (e) {
res.redirect("/");
}
});
app.get("/:id/duplicate", async (req, res) => {
const id = req.params.id;
try {
const doc = await Document.findById(id);
res.render("new", { value: doc.text });
} catch (e) {
res.redirect("/");
}
});