{"size": 1759, "ext": "js", "max_stars_repo_path": "src/handlers/web.js", "max_stars_repo_name": "WietseWind/XRParrot-Backend", "max_stars_repo_stars_event_min_datetime": "2018-11-02T18:19:50.000Z", "max_stars_repo_stars_event_max_datetime": "2019-04-23T12:17:06.000Z", "max_issues_repo_path": "src/handlers/web.js", "max_issues_repo_name": "hbenslama/XRParrot-Backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/handlers/web.js", "max_forks_repo_name": "hbenslama/XRParrot-Backend", "max_forks_repo_forks_event_min_datetime": "2018-12-31T10:46:13.000Z", "max_forks_repo_forks_event_max_datetime": "2019-01-02T20:02:52.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 25.4927536232, "max_line_length": 64, "alphanum_fraction": 0.5923820352}
const express = require('express')
const nunjucks = require('nunjucks')
const bodyParser = require('body-parser')

module.exports = async function (expressApp) {
  expressApp.use(bodyParser.urlencoded({ extended: true }))
  
  /**
   * WEB Router
   */
  const router = express.Router()

  router.get(['/', '/index.html'], (req, res, next) => {
    return res.render('index.html')
  })

  router.get('/*', express.static('public_html'))

  router.get('/about', (req, res, next) => {
    // throw new Error("BROKEN")
    return res.render('about.html')
  })

  // WEBROUTER WILDCARD - FALLBACK
  router.all('*', function(req, res){
    if ('OPTIONS' === req.method) {
      req.session.destroy()
      res.sendStatus(200)
    } else {
      res.status(404).render('404', { error: 'file not found' })
    }
  })

  // Use
  expressApp.use('/web', router)

  /**
   * Template engine
   */
  expressApp.set('view engine', 'html')

  const env = nunjucks.configure('public_html', {
    noCache:  expressApp.config.mode === 'development',
    watch: expressApp.config.mode === 'development',
    autoescape: true,
    express: expressApp
  })

  /**
   * Testing.
   */
  env.addFilter('sleep', function sleep (input, callback) {
    const args = Object.values(arguments).slice(1, -1)
    setTimeout(() => {
      arguments[arguments.length - 1](false, arguments[0])
    }, (args[0] || 1) * 1000)
  }, true)

  env.addFilter('test', function sleep (input, callback) {
    const args = Object.values(arguments).slice(1, -1)
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([ 'a', 'b', 'c', 'd' ])
      }, (args[0] || 1) * 1000)
    }).then(results => {
      arguments[arguments.length - 1](false, results)
    })
  }, true)
}