forked from adithyapaib/linkhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
109 lines (91 loc) · 2.58 KB
/
server.ts
File metadata and controls
109 lines (91 loc) · 2.58 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
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
//Dependencies
import * as express from "express";
import { model, Schema, Model, Document, connect } from 'mongoose';
interface d extends Document {
value: string;
username: string;
}
const documentSchema = new Schema({
value:
{
type: String,
required: true
},
username:
{
type: String
},
});
const G: Model<d> = model('documents', documentSchema)
require("dotenv").config();
//Middle-wares
(async function () {
connect(process.env.DB);
})();
let app = express();
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.static("public"));
var shortid = (length: number, chars: string) => {
var str = "";
if (length == null || typeof length != "number") {
length = 6;
}
if (chars == null) {
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
}
chars.split("");
for (var i = 0; i < length; i++)
str += chars[Math.floor(Math.random() * chars.length)];
return str;
};
// Routes
app.get("/", (req: Request, res: any) => {
res.render("index");
});
app.get("/new", async (req: Request, res: any) => {
let short: string = shortid(6, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz_-");
await res.render("new", { username: short });
});
app.get("/new/:id", async (req: any, res: any) => {
``
let username = await req.params.id;
res.render("new", { username });
});
let detectURLs = async (string: string) =>
await string.replace(
/(\b(https?|ftp|file):\/\/[\-A-Z0-9+&@#\/%?=~_|!:,.;]*[\-A-Z0-9+&@#\/%=~_|])/gim,
'<a href="$1">$1</a><br>'
);
app.post("/save", async (req: any, res: any) => {
let body = await req.body.value, username = await req.body.username;
body = await detectURLs(body)
body = await body.replace(/\n/g, '<br>')
let value = await `<p class="saved">${body} </p>`
try {
let d = await G.create({ username: username, value: value });
return res.redirect(`/${d.username}`);
} catch (err) {
res.render("404")
}
});
app.get("/user/:id", async (req: any, res: any) => {
const username = await req.params.id;
let test = await G.find({ username });
if (test.length > 0) {
res.json(true);
} else {
res.json(false);
}
});
app.get("/:id", async (req: any, res: any, next: express.NextFunction) => {
let id = await req.params.id;
let document = await G.findOne({ username: id });
if (document) {
let code = await document.value;
return await res.render("code", { code, username: id });
} else
return res.render("404");
});
app.get("/*", async (req, res: any) => res.render("404"));
app.listen(3000);