-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
96 lines (76 loc) · 2.72 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
const express = require('express');
const fs = require('fs');
const path = require('path');
const http = require('http');
const helmet = require('helmet');
const { marked } = require('marked');
const app = express();
const port = process.env.PORT || 8080;
const server = http.createServer(app);
const text404 = fs.readFileSync(path.normalize(__dirname + '/src/404.html'), 'utf8');
const pages = {
index: "index.html",
projects: "projects.html",
socials: "socials.html",
blog: "blog.html",
gallery: "gallery.html"
};
app.use(helmet({
hidePoweredBy: true,
noSniff: true,
xssFilter: true,
contentSecurityPolicy: false
}));
app.use(express.static(path.normalize(__dirname + "/src")));
const router = express.Router();
router.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'src', pages.index));
});
router.get('/projects', (req, res) => {
res.sendFile(path.join(__dirname, 'src', pages.projects));
});
router.get('/socials', (req, res) => {
res.sendFile(path.join(__dirname, 'src', pages.socials));
});
router.get('/gallery', (req, res) => {
res.sendFile(path.join(__dirname, 'src', pages.gallery));
});
router.get('/blog', (req, res) => {
const postsDir = path.join(__dirname, 'posts');
fs.readdir(postsDir, (err, files) => {
if (err) {
return res.status(500).send("Error reading posts directory.");
}
const markdownFiles = files.filter(file => file.endsWith('.md'));
fs.readFile(path.join(__dirname, 'src', pages.blog), 'utf8', (err, blogTemplate) => {
if (err) {
return res.status(500).send("Error loading blog page.");
}
const postPromises = markdownFiles.map(file => {
return fs.promises.readFile(path.join(postsDir, file), 'utf8')
.then(markdownContent => {
const postHtml = marked(markdownContent);
return postHtml;
});
});
Promise.all(postPromises)
.then(posts => {
let modifiedBlogTemplate = blogTemplate;
posts.forEach(postHtml => {
modifiedBlogTemplate = modifiedBlogTemplate.replace('<!-- Blog Post Markdown Goes Here -->', postHtml);
});
res.send(modifiedBlogTemplate);
})
.catch(err => {
res.status(500).send("Error processing blog posts.");
});
});
});
});
app.use(router);
app.use((_req, res) => {
res.status(404).send(text404);
});
server.listen(port, () => {
console.log(`Timeless Express is running on http://localhost:${port}`);
});