{"size": 971, "ext": "js", "max_stars_repo_path": "lib/middleware/client-entry.js", "max_stars_repo_name": "Workshape/nimda", "max_stars_repo_stars_event_min_datetime": "2017-02-01T00:19:55.000Z", "max_stars_repo_stars_event_max_datetime": "2017-02-02T13:58:09.000Z", "max_issues_repo_path": "lib/middleware/client-entry.js", "max_issues_repo_name": "Workshape/nimda", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/middleware/client-entry.js", "max_forks_repo_name": "Workshape/nimda", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.2291666667, "max_line_length": 71, "alphanum_fraction": 0.5993820803}
const pug = require('pug')
const path = require('path')
const Promise = require('bluebird')

/**
 * Client Entry middleware
 *
 * Middleware for single-page-app routing - serves client's entry file
 */

const RESERVED_PATHS = [ 'api' ]
const ENTRY_FILE = '../../view/index.pug'

module.exports = function (mandi) {

  /**
   * Respond with index.html file
   *
   * @param  {Object} next
   * @return {void}
   */
  return function * (next) {
    let segments = this.request.path.split('/').splice(1)

    if (RESERVED_PATHS.indexOf(segments[0]) === -1) {
      this.body = yield renderEntryFile()
      return
    }

    yield next
  }

  /**
   * Render entry file
   *
   * @return {Promise}
   */
  function renderEntryFile() {
    return new Promise((resolve, reject) => {
      let fpath = path.resolve(__dirname, ENTRY_FILE)

      pug.renderFile(fpath, { pretty: true, mandi }, (err, result) => {
        err ? reject(err) : resolve(result)
      })
    })
  }

}