lib/middleware/proxy.js
function createProxyHandler (proxies, urlRoot) {
  if (!proxies.length) {
    const nullProxy = (request, response, next) => next()
    nullProxy.upgrade = () => {}
    return nullProxy
  }

  function createProxy (request, response, next) {
    const proxyRecord = proxies.find((p) => request.url.startsWith(p.path))
    if (proxyRecord) {
      log.debug(`proxying request - ${request.url} to ${proxyRecord.host}:${proxyRecord.port}`)
      request.url = request.url.replace(proxyRecord.path, proxyRecord.baseUrl)
      proxyRecord.proxy.web(request, response)
    } else {
      return next()
    }
  }

  createProxy.upgrade = function (request, socket, head) {
    // special-case karma's route to avoid upgrading it
    if (request.url.startsWith(urlRoot)) {
      log.debug(`NOT upgrading proxyWebSocketRequest ${request.url}`)
      return
    }

    const proxyRecord = proxies.find((p) => request.url.startsWith(p.path))
    if (proxyRecord) {
      log.debug(`upgrade proxyWebSocketRequest ${request.url} to ${proxyRecord.host}:${proxyRecord.port}`)
      request.url = request.url.replace(proxyRecord.path, proxyRecord.baseUrl)
      proxyRecord.proxy.ws(request, socket, head)
    }
  }

  return createProxy
}
function createProxyHandler (proxies, urlRoot) {
  if (!proxies.length) {
    const nullProxy = (request, response, next) => next()
    nullProxy.upgrade = () => {}
    return nullProxy
  }

  function createProxy (request, response, next) {
    const proxyRecord = proxies.find((p) => request.url.startsWith(p.path))
    if (proxyRecord) {
      log.debug(`proxying request - ${request.url} to ${proxyRecord.host}:${proxyRecord.port}`)
      request.url = request.url.replace(proxyRecord.path, proxyRecord.baseUrl)
      proxyRecord.proxy.web(request, response)
    } else {
      return next()
    }
  }

  createProxy.upgrade = function (request, socket, head) {
    // special-case karma's route to avoid upgrading it
    if (request.url.startsWith(urlRoot)) {
      log.debug(`NOT upgrading proxyWebSocketRequest ${request.url}`)
      return
    }

    const proxyRecord = proxies.find((p) => request.url.startsWith(p.path))
    if (proxyRecord) {
      log.debug(`upgrade proxyWebSocketRequest ${request.url} to ${proxyRecord.host}:${proxyRecord.port}`)
      request.url = request.url.replace(proxyRecord.path, proxyRecord.baseUrl)
      proxyRecord.proxy.ws(request, socket, head)
    }
  }

  return createProxy
}
function createProxyHandler ( proxies , urlRoot ) { if ( ! proxies . length ) { const nullProxy = ( request , response , next ) => next ( ) nullProxy . upgrade = ( ) => { } return nullProxy } function createProxy ( request , response , next ) { const proxyRecord = proxies . find ( ( p ) => request . url . startsWith ( p . path ) ) if ( proxyRecord ) { log . debug ( ` ${ request . url } ${ proxyRecord . host } ${ proxyRecord . port } ` ) request . url = request . url . replace ( proxyRecord . path , proxyRecord . baseUrl ) proxyRecord . proxy . web ( request , response ) } else { return next ( ) } } createProxy . upgrade = function ( request , socket , head ) { // special-case karma's route to avoid upgrading it if ( request . url . startsWith ( urlRoot ) ) { log . debug ( ` ${ request . url } ` ) return } const proxyRecord = proxies . find ( ( p ) => request . url . startsWith ( p . path ) ) if ( proxyRecord ) { log . debug ( ` ${ request . url } ${ proxyRecord . host } ${ proxyRecord . port } ` ) request . url = request . url . replace ( proxyRecord . path , proxyRecord . baseUrl ) proxyRecord . proxy . ws ( request , socket , head ) } } return createProxy }
Returns a handler which understands the proxies and its redirects, along with the proxy to use
@param proxies An array of proxy record objects
@param urlRoot The URL root that karma is mounted on
@return {Function} handler function
Returns a handler which understands the proxies and its redirects along with the proxy to use