-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (33 loc) · 1.37 KB
/
index.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
'use strict';
/**
* Module dependencies.
*/
const serve = require('koa-static')
const path = require('path')
/* Arguments
* object of hostnames mapped to folders (you need to create folders in static directory with the same names) - required
* path to your static directory - required
* path to default folder (in case if none of hostnames matched this one will be used) - not required
*/
module.exports = (hostnames, static_dir, default_dir = '') => {
return async (ctx, next) => {
try {
// If there is a koa-static middleware get its index
const serve_index = ctx.app.middleware.findIndex(func => func.name === 'serve')
// Use default hostname if no matches
const hostname = hostnames[ctx.hostname] ? hostnames[ctx.hostname] : default_dir
// Compose path to static folder using hostname (must be the same as hostname)
const static_middleware = serve(path.join(static_dir, hostname))
if (serve_index > -1) {
// Replace existing koa-static with the new one
ctx.app.middleware[serve_index] = static_middleware
} else {
// Add new koa-static middleware
ctx.app.use(static_middleware)
}
return await next()
} catch (e) {
ctx.throw(e.status || 500, e.message)
}
}
}