-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathfrontendMiddleware.ts
More file actions
51 lines (44 loc) · 1.79 KB
/
frontendMiddleware.ts
File metadata and controls
51 lines (44 loc) · 1.79 KB
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
import express, { NextFunction, Request, Response } from 'express';
import path from 'path';
import fs from 'fs-extra';
import url from 'url';
import Server from 'Server';
import { frontendBuiltDir } from 'config/paths';
const packageJson = require('../../package.json');
export function frontendMiddleware(server: Server) {
const staticMiddleware = express.static(frontendBuiltDir, {index: false});
if (!fs.pathExistsSync(frontendBuiltDir)) {
server.updateFrontend().catch(console.error);
}
return (req: Request, res: Response, next: NextFunction) => {
staticMiddleware(req, res, err => {
if (err) return next(err);
if (req.method !== 'GET') return next();
const filePath = path.resolve(frontendBuiltDir, 'index.html');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return next(err);
const {pathname} = url.parse(req.originalUrl);
if (!pathname) return next(new Error('Failed to get `pathname`.'));
const [, categoryKey, algorithmKey] = pathname.split('/');
let {title, description} = packageJson;
let algorithm = undefined;
if (categoryKey && categoryKey !== 'scratch-paper') {
algorithm = server.hierarchy.find(categoryKey, algorithmKey) || null;
if (algorithm) {
title = [algorithm.categoryName, algorithm.algorithmName].join(' - ');
description = algorithm.description;
} else {
res.status(404);
return;
}
}
const indexFile = data
.replace(/\$TITLE/g, title)
.replace(/\$DESCRIPTION/g, description)
.replace(/\$ALGORITHM/g, algorithm === undefined ? 'undefined' :
JSON.stringify(algorithm).replace(/</g, '\\u003c'));
res.send(indexFile);
});
});
};
}