nodejs cronjob

Solutions on MaxInterview for nodejs cronjob by the best coders in the world

showing results for - "nodejs cronjob"
Violette
17 Jun 2016
1/**
2 @description CRONJOB RATE LIMIT REQUEST API MIDDLEWARE
3*/
4const cron = require('node-schedule')
5const axios = require('axios')
6
7/**
8 @description store ip address  user to array
9*/
10const IpAddress = []
11
12/**
13 @description fetch data from provider isp
14*/
15const IpGeolocation = async () => {
16  const ip = {}
17  const res = await axios.get('http://ipwhois.app/json/')
18  Object.defineProperty(ip, 'ipv4', { value: res.data.ip, enumerable: true })
19  return ip.ipv4
20}
21
22/**
23 @description reset  ip address after 1 minute
24*/
25const ResetRateLimit = () => {
26  const schedule = new cron.RecurrenceRule()
27  schedule.second = 59
28  schedule.tz = 'asia/jakarta'
29
30  cron.scheduleJob('ResetRateLimit', schedule, () => {
31    IpAddress.splice(0, IpAddress.length)
32  })
33}
34
35module.exports = async (req, res, next) => {
36  /**
37 @description result current IP Location from provider isp
38*/
39  const myIp = await IpGeolocation()
40
41  /**
42   @description get any request method  from user after sending data or request data
43  */
44  switch (req.method) {
45    case 'GET':
46      if (IpAddress.length < 7) {
47        IpAddress.push(myIp)
48      }
49      break
50    case 'POST':
51      if (IpAddress.length < 7) {
52        IpAddress.push(myIp)
53      }
54      break
55    default:
56      return next()
57  }
58
59  /**
60   @description get total requests from the same ip
61  */
62  const currentIp = IpAddress.filter((v, i) => v === myIp).length
63
64  /**
65   @description if the same ip requests more than 5 throw an error message
66  */
67  if (currentIp > 4) {
68    return res.status(401).json({
69      method: req.method,
70      status: res.statusCode,
71      error: 'Oopss..request method not allowed many to request'
72    })
73  }
74
75  /**
76   @description return to next function if  after reset ip address
77  */
78  ResetRateLimit()
79  return next()
80}
queries leading to this page
cronjob node jscron cjob nodenode install cronnode js cron jobnodejs cron job examplecronjob npmjavascript cron jobcron job node js packagecron node moduleexample cron job node jscron javascript libraryhow to use cronjob nodejsnpm module for cron jobnode cron npmcron jon in nodejscronjob with nodejscron npm examplescron node cron npm packagehow to set a cronjob in nodejsnodejs cronjobnode js cron schedulernodecron npmnodejs cronjob mediumhow to use cronjobs in nodecreate cronjob node jscronjob in node jscronejob jsnpm install cronnode js cronjobscron npm installnode cron cronjob node examplenew cronjob running nodejscron in node jscrontab nodejscron running cronjob nodejsnode cron jobscronejob in nodejscron node js all userchron job nodejsnode js croncron module jscronjob con nodeimport cron not woorking nodenode js cronjobcron job nodenodejs croncron job jsnode js cron jobcreate a cron job node jscron job in node jsjavascript cron libsnode cronnode cron schegularcronjob jscron jon in node jscron job node jscronjob nodejscode 3a 27module not found 27 2c cron jscronjob npm promise functionnode js cron jobscronjob arrowsnode cronjobcron job nodejscron node jscron job in nodejscronjod nodejscronjob npm bestcron jscronjob expressnode cron schedulernode crontabnode cron time configurationjavascript croncron job nodejscronjob schedule nodejs formatnodejs crontabcron job node jscron job npm node jsnode in cronjobcronjob node cron job npmcronjob en nodejscron javascriptnpm cron jobs node jsnodejs cronjobnpm library like cron 2cjsnode js cronjobcron nodejsnode cronjob expressioncronjob nodejs examplegood cron npm librarywhat is a cron job node jscron jopb jscron job for nodejscronjob in nodejsnpm run one croncron node npmcron job in nodejs cronjobcron jobs nodehow to apply cronjob on nodejsnode cronjobsnode create cronjobnpm cronjobhow to assign a cronjob to a nodecreate a cronjob in nodejsnodemon cron cron jsnew cron job nodejshow to use cron job in node jsnpm cron jobcronjob start nodejsset chron javascriptnode js cronnode js cron job examplenodejs cron jobnpm run cron jobnpm i cronjavascript use cronnpm cron secondcron in nodejsnode js cron job workjs cron secureisationnodejs cronjobscronjob node jscron npmcron time jscreate cronjob to run nodejsnode cron jobnpm croncron lib javascriptcron job javascriptrun cron job manually npm nodejs node croncron job on node jsnode cron timecronjob node j snode cron examplesnode js cron job conceptnode cron cronjobnodejs cronjob