'use strict'

/**
 * Import npm packages
 */
const express = require('express')
const path = require('path')
const favicon = require('serve-favicon')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const favoritesDb = require('./lib/db/favoritesDb')
const hbs = require('hbs')
hbs.registerPartials(__dirname + '/lib/views/favorites')
require('./lib/views/hbs-helpers')

/**
 * Import local libraries
 */
const routeFootball = require('./lib/routes/football')
const controller = require('./../index')

/**
 * Instantiate...
 */
const app = express()
app.set('views', path.join(__dirname, 'lib/views'))
app.set('view engine', 'hbs')
hbs.localsAsTemplateData(app)

/**
 * Add Middlewares
 */
app.use(favicon(path.join(__dirname, 'public', 'R2D2.ico')))
//app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
app.use((req, res, next) => { 
    res.locals.favoritesList = favoritesDb // Enhance it with per user favorites based on authentication
    next()
})

/**
 * Add controller
 */
const mws = controller('./lib/controllers', {redirectOnStringResult: true})
app.use(mws)

/**
 * Add Routes
 */
app.use('/router', routeFootball) // <=> Route on '/routes' with same features of /football controller  

/**
 * Error-handling middlewares
 */
// catch 404 and forward to default error handler
app.use(function(req, res, next) {
    const err = new Error('Not Found')
    err.status = 404
    next(err)
})

// print stacktrace
app.use(function(err, req, res, next) {
    res.status(err.status || 500)
    res.render('error', {
        message: err.message,
        error: err
    })
})

module.exports = app
@@ -1196,19 +1196,24 @@
 %0D%0Aconst 
-mws
+football
  = contr
@@ -1276,19 +1276,153 @@
 %7D)%0D%0A
-app.use(mws
+const footballApi = controller('./lib/controllers', %7BresultHandler: (res, ctx) =%3E res.json(ctx)%7D)%0D%0Aapp.use(football)%0D%0Aapp.use('/api', footballApi
 )%0D%0A%0D
@@ -1549,18 +1549,16 @@
 ntroller
-  
 %0D%0A%0D%0A/**%0D

Add a new router to example web application, which responds json instead of rendering views.