-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
54 lines (42 loc) · 1.48 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
import { CeramicClient } from "@ceramicnetwork/http-client";
import { TileDocument } from "@ceramicnetwork/stream-tile";
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs";
const PORT = process.env.PORT || 4000;
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.get("/", (req, res) => {
const filePath = path.resolve(__dirname, "./dist", "index.html");
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
return console.error(err);
}
data = data
.replace(/__OG_TITLE__/g, "Headline")
.replace(/__OG_DESCRIPTION__/g, "Headline allows you to own your data")
.replace(/__OG_IMAGE__/g, "/src/assets/favicon.svg");
res.send(data);
});
});
app.get("/pub/*/article/:articleId", async (req, res) => {
const filePath = path.resolve(__dirname, "./dist", "index.html");
const ceramic = new CeramicClient();
const doc = await TileDocument.load(ceramic, req.params.articleId);
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
return console.error(err);
}
data = data
.replace(/__OG_TITLE__/g, `${doc.title}`)
.replace(/__OG_DESCRIPTION__/g, `${doc.description}`)
.replace(/__OG_IMAGE__/g, doc.previewImg);
res.send(data);
});
});
app.use(express.static(path.resolve(__dirname, "./dist")));
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});