forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (48 loc) · 1.11 KB
/
index.js
File metadata and controls
55 lines (48 loc) · 1.11 KB
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
55
'use strict'
/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const serveStatic = require('serve-static')
/**
* serves static files for a given directory
* @class
*/
class Static {
constructor (Helpers, Config) {
const publicPath = Helpers.publicPath()
const options = Config.get('app.static', {})
options.fallthrough = false
this.server = serveStatic(publicPath, options)
}
/**
* serves static file for a given request url
*
* @param {Object} request
* @param {Object} response
* @return {Promise}
*
* @example
* static
* .serve(req, res)
* .then()
* .catch()
* @public
*/
serve (request, response) {
return new Promise((resolve, reject) => {
this.server(request, response, (error) => {
if (!error) {
return resolve()
}
error.message = `Route ${error.message} while resolving ${request.url}`
reject(error)
})
})
}
}
module.exports = Static