UNPKG

1.28 kBJavaScriptView Raw
1'use strict'
2
3/**
4 * Serve a specified source folder.
5 * @public
6 * @param {Array} routes - Array of route configurations.
7 * @param {String} srcPath - Path to the source folder.
8 * @param {Object} opts - Additional optional options.
9 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
10 */
11module.exports = function(routes, srcPath, opts, next) {
12
13 // Require modules on function call to speed up the initial launch
14 const async = require('async')
15 const validate = require('./validate')
16 const rewrite = require('./rewrite')
17 const redirect = require('./redirect')
18 const deliver = require('./deliver')
19
20 // Make opts optional and use opts as next when next is undefined
21 // Next will be validated at a later juncture
22 if (next==null) next = opts
23
24 try {
25
26 next = validate.next(next)
27 routes = routes.map(validate.route)
28 srcPath = validate.path(srcPath)
29 opts = validate.opts(opts)
30
31 } catch (err) {
32
33 return next(err)
34
35 }
36
37 // Handlers may use promises which could lead to unhandled rejections
38 process.on('unhandledRejection', next)
39
40 const _rewrite = rewrite(routes, srcPath)
41 const _redirect = redirect()
42
43 async.series([
44
45 (next) => deliver(srcPath, _rewrite, _redirect, opts, next)
46
47 ], next)
48
49}
\No newline at end of file