lib/throttler.js
function throttleMiddleware (request, response, next) {
    var ip = null

    if (config.useCustomHeader && request.headers[config.useCustomHeader]) {
      ip = request.headers[config.useCustomHeader]
    } else {
      ip = request.headers['x-forwarded-for'] ||
        request.connection.remoteAddress ||
        request.socket.remoteAddress ||
        request.connection.socket.remoteAddress || ''

      ip = ip.split(',')[0]
      if (!ipRegex().test(ip) || ip.substr(0, 7) === '::ffff:' || ip === '::1') {
        ip = '127.0.0.1'
      } else {
        ip = ip.match(ipRegex())[0]
      }
    }

    Throttle
      .findOneAndUpdate({ip: ip}, { $inc: { hits: 1 } }, { upsert: false })
      .exec(function (error, throttle) {
        if (errHandler(response, next, error)) { return }
        if (!throttle) {
          throttle = new Throttle({
            createdAt: new Date(),
            ip: ip
          })
          throttle.save(function (error, throttle) {
            // THERE'S NO ERROR, BUT THROTTLE WAS NOT SAVE
            if (!throttle && !error) {
              error = new Error('Error checking rate limit.')
            }

            if (errHandler(response, next, error)) { return }

            respondWithThrottle(request, response, next, throttle)
          })
        } else {
          respondWithThrottle(request, response, next, throttle)
        }
      })
  }
function throttleMiddleware (request, response, next) {
    var ip = null

    if (config.useCustomHeader && request.headers[config.useCustomHeader]) {
      ip = request.headers[config.useCustomHeader]
    } else {
      ip = request.headers['x-forwarded-for'] ||
        request.connection.remoteAddress ||
        request.socket.remoteAddress ||
        request.connection.socket.remoteAddress || ''

      ip = ip.split(',')[0]
      if (!ipRegex().test(ip) || ip.substr(0, 7) === '::ffff:' || ip === '::1') {
        ip = '127.0.0.1'
      } else {
        ip = ip.match(ipRegex())[0]
      }
    }

    Throttle
      .findOneAndUpdate({ip: ip}, { $inc: { hits: 1 } }, { upsert: false })
      .exec(function (error, throttle) {
        if (errHandler(response, next, error)) { return }
        if (!throttle) {
          throttle = new Throttle({
            createdAt: new Date(),
            ip: ip
          })
          throttle.save(function (error, throttle) {
            // THERE'S NO ERROR, BUT THROTTLE WAS NOT SAVE
            if (!throttle && !error) {
              error = new Error('Error checking rate limit.')
            }

            if (errHandler(response, next, error)) { return }

            respondWithThrottle(request, response, next, throttle)
          })
        } else {
          respondWithThrottle(request, response, next, throttle)
        }
      })
  }
function throttleMiddleware ( request , response , next ) { var ip = null if ( config . useCustomHeader && request . headers [ config . useCustomHeader ] ) { ip = request . headers [ config . useCustomHeader ] } else { ip = request . headers [ 'x-forwarded-for' ] || request . connection . remoteAddress || request . socket . remoteAddress || request . connection . socket . remoteAddress || '' ip = ip . split ( ',' ) [ 0 ] if ( ! ipRegex ( ) . test ( ip ) || ip . substr ( 0 , 7 ) === '::ffff:' || ip === '::1' ) { ip = '127.0.0.1' } else { ip = ip . match ( ipRegex ( ) ) [ 0 ] } } Throttle . findOneAndUpdate ( { ip : ip } , { $inc : { hits : 1 } } , { upsert : false } ) . exec ( function ( error , throttle ) { if ( errHandler ( response , next , error ) ) { return } if ( ! throttle ) { throttle = new Throttle ( { createdAt : new Date ( ) , ip : ip } ) throttle . save ( function ( error , throttle ) { // THERE'S NO ERROR, BUT THROTTLE WAS NOT SAVE if ( ! throttle && ! error ) { error = new Error ( 'Error checking rate limit.' ) } if ( errHandler ( response , next , error ) ) { return } respondWithThrottle ( request , response , next , throttle ) } ) } else { respondWithThrottle ( request , response , next , throttle ) } } ) }
Check for request limit on the requesting IP

@access public
@param {object} request Express-style request
@param {object} response Express-style response
@param {function} next Express-style next callback
Check for request limit on the requesting IP