lundi 25 juin 2018

Delay treatment with routes in Node JS

I'm working on a web application with NodeJs and I need to send a file on a FTP server. I putted a timer so each request will put their data in the file and every 30s, one of the request in my main route will send it on. I've used this trick because I can't know the last request that will be made (for example I have 50 000 requests, my file will be updated every 30s with informations but the last 30s won't be triggered). My question is : " Can I make a treatment outside my route ?". I thought maybe something in my main route could trigger a reset of my timer so every time my main route is called, the second one is delayed for 30s. By that trick, the last few requests won't be skipped. Thanks and don't hesitate to ask me questions if it is not clear.

The structure of my main route :

var dateServer = new Date();
...
app.post('/activity/execute', (req, res) => {
    console.log('EXECUTE');
    //DO THINGS
    let dateContact = new Date();
    let diffTime = dateContact.getSeconds()- dateServer.getSeconds();
    if (diffTime >= 30 || diffTime <= -30) {
       dateServer = dateContact;
       //DO THINGS
    }

}




Aucun commentaire:

Enregistrer un commentaire