UNPKG

1.15 kBJavaScriptView Raw
1'use strict'
2
3const log = require('./log')
4
5/**
6 * Executes a handler.
7 * @public
8 * @param {Object} route - A single route configuration.
9 * @param {String} fileRoute - Relative path to a file (can be fictive).
10 * @param {String} filePath - Absolute path to a file (must exist).
11 * @param {?Boolean} optimize - Optimize option. Will be passed to handler when route opts don't contain optimize option.
12 * @param {Function} next - The callback that handles the response. Receives the following properties: err, data.
13 */
14module.exports = function(route, fileRoute, filePath, optimize, next) {
15
16 log(`{cyan:Starting handler: {magenta:${ route.name } {grey:${ fileRoute }`)
17
18 const processResolve = (data) => {
19
20 log(`{cyan:Finished handler: {magenta:${ route.name } {grey:${ fileRoute }`)
21
22 next(null, data)
23
24 }
25
26 const processReject = (err) => {
27
28 log(`{red:Handler failed: {magenta:${ route.name } {grey:${ fileRoute }`)
29
30 return next(err)
31
32 }
33
34 const opts = Object.assign({}, route.opts, {
35 optimize: (route.opts.optimize===undefined ? optimize : route.opts.optimize)
36 })
37
38 route
39 .handler(filePath, opts)
40 .then(processResolve, processReject)
41
42}
\No newline at end of file