This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
tstatic/src/middleware/static-files.ts

28 lines
700 B
TypeScript

import express, { Request, Response } from 'express';
import serveIndex from 'serve-index';
import path from 'path';
function isDirectory(url : string) : boolean {
return /\/$/.test(url);
}
export function indexHandle(request : Request, response : Response, next : Function) {
if (isDirectory(request.url)) {
request.url = path.join(request.url, 'index.html');
}
return next();
}
export function staticFileHandle(serveDir : string) {
return express.static(serveDir, {
dotfiles: 'ignore',
index: false,
redirect: true
});
}
export function serveIndexHandle(serveDir : string) {
return serveIndex(serveDir, {
icons: true
});
}