express-static-search
v0.1.9
Published
Module to get static files with deep scan of relative paths.
Downloads
12
Maintainers
Readme
express-static-search
Module to get static files with deep scan of relative paths.
Install
npm install express-static-search
Example
const expressStatic = require("express-static-search");
const express = require("express");
const path = require("path");
const app = express();
const staticPath = path.join(__dirname, 'assets');
const options = {
beforeSend: (res, fileName) => {
if(fileName.match('is-not-static.js')) {
return false; // return false to cancel responding of this file
}
res.setHeader('Cache-Control', 'public, max-age=3600');
}
}
app.use(expressStatic(staticPath, options));
Description
Let's say client is on /hello/world/ page and requests static-file.mp3. It is relative path, so browser sends to the server full url /hello/world/static-file.mp3. In some cases we can't set absolute path, but want to get the necessary file. This library scans every part of url to find a match. It will try to get:
- staticPath + /hello/world/static-file.mp3
- staticPath + /world/static-file.mp3
- staticPath + static-file.mp3
If any file of the specified exists, then you will get it.