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:
- Pls what approach do you think would be the best to achieve this?
- 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.
app.get("/", (req, res) => { schedule.scheduleJob("1-20 * * * * *", async () => { res.status(400) }) res.status(200).json({ msg: "You're in!!" }) })
Advertisement
Answer
you can use a functions to check if the date is in that range
const isClosed = (date) => date.getDay() === 4 && date.getHours() === 23 && date.getMinutes()>= 55 && date.getMinutes()<= 59 console.log(isClosed(new Date('2022-08-25 23:55:01')))
and then you can use a middleware to block the request like this
const isClosedMiddleware = (req, res, next) => { const date = new Date() if(isClosed(date)){ res.status(400) } else { next() } } app.get("/", isClosedMiddleware, (req, res) => { res.status(200).json({ msg: "You're in!!" }) })