-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (75 loc) · 2.56 KB
/
app.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
let express = require("express");
let path = require("path");
let fs = require("fs");
let frontMatter = require("front-matter");
let nunjucks = require("nunjucks");
let app = express();
app.listen(4001, () => { console.log("Server listening on 4001 port"); });
//nunjucks config
nunjucks.configure('views', { express: app });
//use statics
app.use("/assets", express.static("static"));
//favicon without middleware
app.use('/favicon.ico', express.static('favicon.ico'));
//get encoded data
app.use(express.urlencoded());
//list all blogs
app.get('/', function (req, res) {
let blogPath = path.join(__dirname + '/blog/');
let filenames = fs.readdirSync(blogPath);
function readFromFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(blogPath + file, 'utf-8', function (err, data) {
if (err) {
console.log('err', err);
return reject(err);
}
let raw = frontMatter(data);
file = file.split('.').shift();
raw.link = '/blog/' + file;
return resolve(raw);
});
});
}
Promise.all(filenames.map(item => readFromFile(item))).then((results) => {
return res.render('index.html', {
data: results
});
});
});
app.use('/blog/*', (req, res) => {
let mdPath = path.join(__dirname + req.baseUrl + '.md');
fs.readFile(mdPath, "utf-8", (err, data) => {
if (err) {
console.error(err);
return;
}
let raw = frontMatter(data);
return res.render(raw.attributes.layout + '.html', { title: raw.attributes.title, data: raw.body });
});
});
//form requests
app.post('/form', function (req, res) {
let formsPath = path.join(__dirname + '/forms/');
//using slugify function for filename standardization
let slugify = (string) => {
const a = 'àáäâãåăæçèéëêǵğḧìíïîıḿńǹñòóöôœṕŕßśşțùúüûǘẃẍÿź·/_,:;';
const b = 'aaaaaaaaceeeegghiiiiimnnnoooooprssstuuuuuwxyz------';
const p = new RegExp(a.split('').join('|'), 'g');
let url = string.toString().toLowerCase()
.replace(/\s+/g, '-') // replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // replace special characters
.replace(/&/g, '-and-') // replace & with 'and'
.replace(/[^\w\-]+/g, '') // remove all non-word characters
.replace(/\-\-+/g, '-') // replace multiple - with single -
.replace(/^-+/, '') // trim - from start of text
.replace(/-+$/, ''); // trim - from end of text
return url;
};
let fileName = slugify(req.body.name);
if (req.body.name && req.body.surname) {
fs.writeFile(formsPath + `${fileName}.txt`, req.body.message, function (err) {
if (err) throw err;
});
}
});