Skip to content
Advertisement

ShouldI replace setTimeout with node schedule in nodejs

I want to stop players from entering a raffle starting from 11:55pm – 11:59pm every thursday, so I have to make sure the route for entering the raffle at this particular time is block. I decided to use node-schedule for every seconds to see what the result would look like, but I was able to access the route. I dont know if node-schedule is suitable for this kind of thing or if I should use a setTimeout that runs every seconds in the server so I can check if its time to restrict players.

My questions are:

  1. Pls what approach do you think would be the best to achieve this?
  2. Will scheduling a job or using a setTimeout that run every seconds/minutes in my server breaks it.

Here is the code I wrote for restricting user from an endpoint at some particular seconds, but it is not working.

JavaScript

Advertisement

Answer

you can use a functions to check if the date is in that range

JavaScript

and then you can use a middleware to block the request like this

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement