-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstatic.es.js
54 lines (52 loc) · 1.9 KB
/
static.es.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cacheable from './cacheable.js';
import live from './live.js';
/**
* Serves file
* @param {String} path Path of folder
* @param {object} options Options of serve
* @param {String=} options.mode Serve mode, has two values - Cached and Live.
* Cached gives better performance, but on refresh gives cached result
* and uses more RAM.
* Live mode uses less memory usage, on request responses last version
* but on high-load applicatins may reduces performance
*
* @param {Boolean=} options.index Index filename
* @param {Boolean=} options.forcePretty Force appending index-file
* path even path isn't as root
*
* @param {Boolean=} options.addPrettyUrl Enable pretty url
* by auto-appending index-file, so works `/` like `/index.html`
*
* @param {Boolean=} options.lastModified Enable last-modified check,
* if file not modified, returns empty request with 304 status
* @param {Boolean=} options.compressed Compress response/response streams
* @default options.mode cached `Uses cached mode by default`
* @default options.index index.html `Default index.html as index-file`
* @default options.forcePretty false `Disabled by default`
* @default options.addPrettyUrl true `Enabled by default`
* @default options.lastModified true `Enabled by default`
* @default options.compressed true `Enabled by default`
*
* @example // If mode is `cached`
* app.use(await staticServe('./assets/static'))
* @example // If mode is `live`
* app.use(staticServe('./assets/static'))
*/
export default function staticServe(path, config = {}) {
config = {
mode: 'cached',
index: 'index.html',
forcePretty: false,
addPrettyUrl: true,
lastModified: true,
compressed: true,
...config
};
if (config.mode === 'live') {
return live(path, config);
}
if (config.mode === 'cached') {
return cacheable(path, config);
}
throw new Error('[nanoexpress::Middlewares]: {static} Unknown mode');
}