-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (121 loc) · 3.63 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
const path = require("path");
const Paste = require("./models/paste");
const app = express();
const PORT = process.env.PORT || 3000;
const MONGO_URI =
process.env.MONGO_URI || "mongodb://127.0.0.1:27017/paste-service";
const connectDB = async () => {
try {
const conn = await mongoose.connect(MONGO_URI);
console.log(`MongoDB connected : ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
app.use(express.static(path.join(__dirname, "static")));
class ExpressError extends Error {
constructor(message, status) {
super();
this.status = status;
this.message = message;
this.byuser = true;
}
}
app.get("/", async (req, res) => {
res.render("home");
});
app.post("/", async (req, res, next) => {
try {
const { text } = req.body;
if (!text) {
throw new ExpressError("Invalid text input", 400);
}
const restricted = req.body.restricted ? true : false;
let expireDuration = parseInt(req.body.expireDuration);
const Text = new Paste({ text, expireDuration, restricted });
await Text.save();
res.redirect(`/view/${Text.address}`);
} catch (e) {
next(e);
}
});
app.get("/view/:id", async (req, res, next) => {
try {
const { id } = req.params;
const Text = await Paste.findOne({ address: id });
if (!Text) {
throw new ExpressError("Cannot find the paste link", 404);
}
const isAlive = await Text.alive();
if (isAlive) {
return res.render("view", { Text });
}
throw new ExpressError("Link Expired or don't exists", 404);
} catch (e) {
next(e);
}
});
app.get("/edit/:id", async (req, res, next) => {
try {
const { id } = req.params;
const Text = await Paste.findOne({ address: id });
if (!Text.restricted) {
return res.redirect(`/view/${id}`);
}
if (!Text) {
throw new ExpressError("Cannot find the paste link", 404);
}
const isAlive = await Text.alive();
if (isAlive) {
return res.render("edit", { Text });
}
return res.send("Expired");
} catch (e) {
next(e);
}
});
app.patch("/edit/:id", async (req, res, next) => {
try {
const { id } = req.params;
const { text } = req.body;
const Text = await Paste.findOne({ address: id });
if (!Text) {
throw new ExpressError("Cannot find the paste link", 404);
}
await Paste.updateOne({ address: id }, { text });
res.redirect(`/view/${id}`);
} catch (e) {
next(e);
}
});
app.get("*", (req, res) => {
throw new ExpressError("Page not found", 404);
});
app.use((err, req, res, next) => {
console.log(err.message);
if (err.byuser) {
return res.status(err.status).render("error", { err });
}
err.status = 500;
err.message = "Something went wrong.";
res.status(err.status).render("error", { err });
});
connectDB()
.then(() => {
app.listen(3000, () =>
console.log(`LISTENING ON PORT 3000\nhttp://localhost:${PORT}`)
);
})
.catch((err) => {
console.log("This part got hit.\n");
console.log(err);
});