evolution-manager/lib/api/view.router.ts.patch
2023-11-19 18:26:13 -03:00

41 lines
1.2 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 baseDir = __dirname.includes('dist') ? '../../../../' : '../../../';
const indexPath = path.join(__dirname, baseDir, 'Extras/evolution-manager', 'index.html');
const index = fs.readFileSync(indexPath);
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, baseDir, '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();
}