Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

express rate limit redis

import { Request, Response, NextFunction } from 'express'
import IORedis from 'ioredis'
import ip from 'request-ip'

let io = new IORedis({
	host: process.env.REDIS_HOST || 'localhost',
	port: parseInt(process.env.REDIS_PORT || '')
})

export async function rateLimiterById(req: Request, res: Response, next: NextFunction): Promise<any> {
	// store id to redis
	await io.set(`redis-id:${req.payload.uid}`, req.payload.uid)
	// get request by id
	const getId = await io.get(`redis-id:${req.payload.uid}`)
	// counter count request
	const maxCounterRequest = await io.incrby(`counter-id:${req.payload.uid}`, 1)

	if (getId === req.payload.uid && maxCounterRequest <= 50) {
		await io.expire(`counter-id:${req.payload.uid}`, 10)
	} else {
		await io.del(`redis-id:${req.payload.uid}`)
		return res.status(429).json({
			status: 'ERROR TO MANY REQUEST',
			code: 'AX2AC5R',
			message: 'cannot access this endpoint, after 10 second is over'
		})
	}

	next()
}

export async function rateLimiterByIp(req: Response, res: Response, next: NextFunction): Promise<any> {
	const getIp = ip.getClientIp(req)
	// store id to redis
	await io.set(`redis-ip:${getIp}`, `${getIp}`)
	// get request by id
	const getStoreIp = await io.get(`redis-ip:${getIp}`)
	// counter count request
	const maxCounterRequest = await io.incrby(`counter-ip:${getIp}`, 1)

	if (getStoreIp === getIp && maxCounterRequest <= 50) {
		await io.expire(`counter-ip:${getIp}`, 10)
	} else {
		await io.del(`redis-ip:${getIp}`)
		return res.status(429).json({
			status: 'ERROR TO MANY REQUEST',
			code: 'AX2AC5R',
			message: 'cannot access this endpoint, after 10 second is over'
		})
	}

	next()
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js 10.2 * 100 result of 10.199999 
Javascript :: jquery unobtrusive validation asp.net core 
Javascript :: kjkjl 
Javascript :: how to delete an item on click in js 
Javascript :: eeeeee 
Javascript :: javascript relational operators 
Javascript :: filter data from database for specific user in js 
Javascript :: how to send authorization in header of HTTP GET using ajax 
Javascript :: redux acions 
Javascript :: node-google-spreadsheet color border 
Javascript :: npm can i use my modules without specifying the path 
Javascript :: javascript curtocircuito 
Javascript :: namesandroles javascript 
Javascript :: quasar composition api $q 
Javascript :: var test 
Javascript :: list pci express version command line 
Javascript :: [ERROR -]<ng-select {{items}}="cities" 
Javascript :: thunk sintaxe 
Javascript :: render blaze in react 
Javascript :: res : [ Circular ] nodejs 
Javascript :: lookupedit devexpress get specific row 
Javascript :: react addon update 
Javascript :: Cypress.currentTest 
Javascript :: $(document).ready(function() { $(".menu-icon").on("click", function() { $("nav ul").toggleClass("showing"); }); }); 
Javascript :: react $ r component instance console 
Javascript :: how to convert json to bootstrap treeview format 
Javascript :: javascript substring messes emoji 
Javascript :: how to get 3rd level form data by $refs in vue 
Javascript :: image image using next and previous button in javascript 
Javascript :: jquery copier dans le presse papier 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =