-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
214 lines (198 loc) · 5.85 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const http = require("http"),
express = require("express"),
app = express(),
path = require("path"),
fs = require('fs'),
matter = require('gray-matter');
var bodyParser = require("body-parser");
//Function and other
function fc(filee) {
return fs.readdirSync(__dirname + `/views/library/${filee}`).length - 1;
}
// app setup
app.set('views', path.join(__dirname, 'views'));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.use('/public', express.static('public'));
app.use(express.static(__dirname + '/public'));
app.get("/", function(req, res) {
res.render("index", {
url: req.url
});
});
/* [ Maintenance Management ] */
/*
app.use(function(req, res, next) {
res.status(404);
if (req.accepts('html')) {
res.render('component/maintenance', { exp: "Sep 30, 2021 00:00:00" });
return;
}
});*/
//Routing
app.get("/library/:am/:im", (req, res) => {
function file() {
return matter.read(__dirname + `/views/library/${req.params.am}/${req.params.im}` + '.md');
};
const page = req.params.im || 1
const perPage = 3
res.render(`blog`, {
url: req.url,
post: file().content,
title: file().data.title,
description: file().data.description,
author: file().data.author,
date: file().data.date,
current: page,
pages: fc(req.params.am),
blog: req.params.am
});
});
app.get("/search", (req, res) => {
var q = req.query.q;
let end = [];
const folders = fs.readdirSync(__dirname + `/views/library`, { withFileTypes: true }).map(x => x.name)
let outp;
folders.forEach(folder => {
let fol = fs.readdirSync(__dirname + `/views/library/${folder}`).filter(x => x !== "0.md")
fol.forEach(name => {
const mat = matter.read(__dirname + `/views/library/${folder}/${name}`)
end.push({
"title": mat.data.title,
"description": mat.data.description,
"author": mat.data.author,
"date": mat.data.date,
"image": mat.data.image,
"url": "https://fyy.my.id/library/" + folder + "/" + name.slice(0, -3),
"keyword": "" + mat.data.title
});
});
});
let end_s;
if (q) {
end_s = end.filter(x => x.keyword.toLowerCase().includes(q.toLowerCase()));
}
else {
end_s = end;
};
let content = " ";
for (const ends of end_s) {
content += `<article class='hentry'>
<div class='postThumbnail'>
<a href='${ends.url}'>
<img alt='${ends.title}' class='imgThumb lazy' data-src='${ends.image}' src='data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='/>
</a>
</div>
<div class='postContent'>
<div class='postHeader'>
<div class='postLabel' data-text='in'>
<a aria-label='${ends.title}' data-text='${ends.title}' href='${ends.url}' rel='tag'>
</a>
</div>
</div>
<h2 class='postTitle'>
<a href='${ends.url}' rel='bookmark'>
${ends.title}
</a>
</h2>
<div class='postInfo'>
<time class='postTimestamp updated' data-text='By ${ends.author} — ${ends.date}'></time>
</div>
</div>
</article>`
}
res.render(`search`, {
content: content,
title: end_s.title,
query: q || " ",
author: end_s.author,
description: end_s.description,
});
});
app.get("/library/:im", (req, res) => {
res.render(`blog-parent`, {
post: file().content,
title: file().data.title,
description: file().data.description,
author: file().data.author,
date: file().data.date,
page: req.params.im
});
function file() {
let fold = '' + req.params.im
if (req.params.im == "search") fold = 'because-i-like-you'
return matter.read(__dirname + `/views/library/${fold}/0` + '.md');
}
});
app.get("/library", (req, res) => {
//. Jangan tambahin code dibawah sini ._.
res.render("library", {
blog: function(input) {
let abc = fs.readdirSync(__dirname + '/views/library').filter(x => x.startsWith(input))
let result = " ";
if (abc.length > 0) {
for (b of abc) {
let bb = ''+b
result += `<div class='win-item'><div class='win-title'><a title='Because I Like You' class="link-info" href='https://fyy.my.id/library/${bb}'>${upLet(bb.replace(/-/gi,' '))}</a></div></div>`
}
}; //`
return result;
function upLet(words) {
var separateWord = words.toLowerCase().split(' ');
for (var i = 0; i < separateWord.length; i++) {
separateWord[i] = separateWord[i].charAt(0).toUpperCase() +
separateWord[i].substring(1);
}
return separateWord.join(' ');
};
}
//`
});
});
//AD for Raznar
app.get("/ad1", (req, res) => {
res.render("component/test")
})
app.get("/ad2", (req, res) => {
res.render("component/test1")
})
app.get("/ad3", (req, res) => {
res.render("component/test2")
})
app.get("/tes", (req, res) => {
res.render("blogt")
})
app.get("/p/:year/:month/:id", (req, res) => {
res.render(`post/${req.params.year}/${req.params.month}/${req.params.id}`) || res.render("component/404");
});
/*
app.use((req, res, next) => {
const error = new Error("Not found");
error.status = 404;
next(error);
});
// error handler middleware
app.use((error, req, res, next) => {
res.status(error.status || 500).render(`component/error`, {
status: error.status || 500,
message: error.message || 'Internal Server Error',
});
});*/
var port = process.env.PORT || 8080;
console.info(`Listening to http://localhost:${port}`)
app.listen(port);
/*. DONT TOUCH THIS CODE */
process.on("uncaughtException", (err) => {
console.log("Uncaught Exception: " + err);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
console.log(
"[FATAL] Possibly Unhandled Rejection at: Promise ",
promise,
" reason: ",
reason.message
);
});