 
     @context: IronRouter.Router.route()
   ###
-  _respond = (route, body, statusCode=200, headers) ->
+  _respond: (endpointContext, body, statusCode=200, headers) ->
     # Allow cross-domain requests to be made from the browser
-    @response.setHeader 'Access-Control-Allow-Origin', '*'
+    endpointContext.response.setHeader 'Access-Control-Allow-Origin', '*'
 
     # Ensure that a content type is set (will be overridden if also included in given headers)
     # TODO: Consider enforcing a text/json-only content type (override any user-defined content-type)
-    @response.setHeader 'Content-Type', 'text/json'
+    endpointContext.response.setHeader 'Content-Type', 'text/json'
 
     # Prettify JSON if configured in API
-    if route.api.config.prettyJson
+    if @api.config.prettyJson
       bodyAsJson = JSON.stringify body, undefined, 2
     else
       bodyAsJson = JSON.stringify body
 
     # Send response
-    @response.writeHead statusCode, headers
-    @response.write bodyAsJson
-    @response.end()+    endpointContext.response.writeHead statusCode, headers
+    endpointContext.response.write bodyAsJson
+    endpointContext.response.end()

    Must be called after _authAccepted().

    @context: IronRouter.Router.route() (after authentication)
    @returns True if the authenticated user belongs to <i>any</i> of the acceptable roles on the endpoint
  ###
  _roleAccepted = (route, endpoint) ->
    if endpoint.roleRequired
      if _.isEmpty _.intersection(endpoint.roleRequired, @user.roles)
        return false
    true


    @param {Route} route The route the endpoints belong to
    @param {Endpoint} endpoint The endpoint to configure
  ###
  _configureEndpoints: ->
    _.each @endpoints, (endpoint) ->
        # Configure acceptable roles
      if not @options?.roleRequired
        @options.roleRequired = []
      if not endpoint.roleRequired
        endpoint.roleRequired = []
      endpoint.roleRequired = _.union endpoint.roleRequired, @options.roleRequired
      # Make it easier to check if no roles are required
      if _.isEmpty endpoint.roleRequired
        endpoint.roleRequired = false

      # Configure auth requirement
      if not @api.config.useAuth
        endpoint.authRequired = false
      else if endpoint.authRequired is undefined
        if @options?.authRequired or endpoint.roleRequired
          endpoint.authRequired = true
        else
          endpoint.authRequired = false
      return
    , this
    return


