-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (52 loc) · 1.58 KB
/
server.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
const express = require("express");
const app = express();
require("dotenv").config();
const bodyParser = require("body-parser");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: false }));
const Document = require("./models/Document");
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGODB_URI);
app.get("/", (req, res) => {
const text =
"Hi! You're on Kyoyu. Click new to create a new document and share it with others.";
res.render("display", { text, language: "plaintext" });
});
app.get("/new", (req, res) => {
res.render("new");
});
app.post("/save", async (req, res) => {
// try {
// const value = req.body.value;
// console.log(value);
// } catch (error) {
// console.log("Value is undefined");
// }
const value = req.body.value;
try {
const document = await Document.create({ value });
res.redirect(`${document.id}`);
} catch (error) {
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 (error) {
res.redirect(`/${id}`);
}
});
app.get("/:id", async (req, res) => {
const id = req.params.id;
try {
const document = await Document.findById(id);
res.render("display", { text: document.value, id });
} catch (error) {
res.redirect("/");
}
});
app.listen(process.env.PORT || 3000);