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
|
const express = require("express");
const ejs = require("ejs");
const fs = require("fs");
const glob = require("glob");
const loadArticle = require("../load-article.js");
const multer = require("multer");
const path = require("path");
const app = express();
app.use("/editor", express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "../public")));
app.get("/", (req, res) => {
glob("articles/*.md", (err, articles) => {
if (err) { throw err; }
Promise.all(articles.map(loadArticle)).then(articles => {
res.end(
ejs.render(
fs.readFileSync(
path.join(__dirname, "../templates/layout.ejs"),
"utf-8"
),
{
articles: articles,
title: "New post | Fuwn",
description: "A new post",
isEditor: true,
body: ejs.render(
fs.readFileSync(
path.join(__dirname, "../templates/article.ejs"),
"utf-8"
),
{
timeless: false,
hidden: false,
date: "January 01, 1970",
title: "New post",
tags: [],
body: "<p>Start writing...</p>",
tweetUrl: "",
}
),
}
)
);
});
});
});
const upload = multer();
app.post("/save", upload.array(), (req, res) => {
const fileContents = [
"---",
`title: ${req.body.title}`,
`route: ${req.body.route}`,
`date: ${req.body.date}`,
`description: ${req.body.description}`,
"---",
"",
req.body.content.replace(/\r\n/g, "\n"),
].join("\n");
fs.writeFile(
path.join(__dirname, "../articles", req.body.filename),
fileContents,
(err, data) => {
if (err) {
res.sendStatus(500);
res.end("Sorry :(");
}
res.end("Saved!");
}
);
});
console.log("Listening on http://localhost:8080...");
app.listen(8080);
|