From 9cbae3b8a1b12d8e5a977d8b76aae83065b9af67 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 15 Feb 2017 20:18:40 +0000 Subject: [PATCH] Added static file middleware --- src/middleware/static-files.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/middleware/static-files.ts diff --git a/src/middleware/static-files.ts b/src/middleware/static-files.ts new file mode 100644 index 0000000..4642a47 --- /dev/null +++ b/src/middleware/static-files.ts @@ -0,0 +1,22 @@ +import express, { Request, Response } from 'express'; +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 + }); +}