1
0

31 lines
862 B
TypeScript

import express from 'express';
import fs from 'fs';
import url from 'url';
const router = express.Router();
/* This router only handles HTML files and is used
* to replace BASEPATH */
router.get("/*", (req, res, next) => {
const parts = url.parse(req.url),
basePath = req.app.get("basePath") as string;
if (!/^\/[^/]+\.html$/.exec(parts.pathname || '')) {
return next();
}
console.log("Attempting to parse 'frontend" + parts.pathname + "'");
/* Replace <script>'<base href="/BASEPATH/">';</script> in index.html with
* the basePath */
fs.readFile("frontend" + (parts.pathname || ''), "utf8", function(error, content) {
if (error) {
return next();
}
res.send((content as string).replace(
/<script>'<base href="BASEPATH">';<\/script>/,
"<base href='" + basePath + "'>"));
});
});
export default router;