'How to avoid spamming a NodeJS function? [closed]

So basicly I have a contact form in my Express application what sends me an email with SMTP. I want to do some checks in the server-side to avoid spamming. What do you advise?

Earlier today somebody just used ~100times in just minutes, probably my mail server banned it after that's why he stopped.



Solution 1:[1]

https://www.npmjs.com/package/express-rate-limit works well. I could set a limit for the contact form's request also another lighter limit for the whole website.

Thanks callmemath in the comments for mentioning rate limitations.

Solution 2:[2]

You can use node-schedule package from npm node-schedule, it will help you to schedule cron jobs whenever you want to send email.

const schedule = require('node-schedule')

const job = schedule.scheduleJob('21 * * * *', function(){
  console.log('Send my email.')
})

This will excute this cron at exactly 21min of any hour.

You can go through the npm package for more scheduling details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 callmemath
Solution 2 Ashishssoni