evolution-manager/lib/api/view.router.ts.patch
2023-11-19 15:01:19 -03:00

40 lines
1.1 KiB
Diff

import { Router } from 'express';
import fs from 'fs';
import mime from 'mime-types';
import path from 'path';
import { RouterBroker } from '../abstract/abstract.router';
export class ViewsRouter extends RouterBroker {
constructor() {
super();
const index = fs.readFileSync(path.join(__dirname, '../../../', 'Extras/evolution-manager', 'index.html'));
this.router.get('/*', (req, res) => {
try {
const pathname = req.url.split('?')[0];
// verify if url is a file in dist folder
if (pathname === '/') throw {};
const filePath = path.join(__dirname, '../../../', 'Extras/evolution-manager', pathname);
if (fs.existsSync(filePath)) {
const contentType = mime.lookup(filePath) || 'text/plain';
res.set('Content-Type', contentType);
res.end(fs.readFileSync(filePath));
return;
}
res.set('Content-Type', 'text/html');
res.send(index);
} catch {
res.set('Content-Type', 'text/html');
res.send(index);
}
});
}
public readonly router = Router();
}