1
votes

I have an API endpoint that accepts some data from the client. There is also a 1 minute timer which is visible to the client. What I hope to achieve is this:

Whilst the timer is active ( > 0 ) any posts sent to the API are kept in storage or in array ( or something ). Once the timer reaches zero, The client can no longer make a request to the API and any requests that were made and stored whilst the timer was active, are now processed through a function - For sake of example lets just say that this function logs all the data to the screen.

Perhaps i'm thinking of this in the wrong way, but how do I sync a front and backend timer so both the server and the client side know when to stop processing POST requests and to let the server know that it's time to process all the data that was sent during that 1 minute.


    var express = require("express");
    var app = express();

    app.post("/api/data", function(req, res){
    //   do something here - no clue
    });

    app.listen(process.env.PORT, () => {
        console.log("server running on port: ", process.env.PORT);
    });

Apologies if I've explained this poorly. Appreciate any help I can get, thank you :)

2

2 Answers

1
votes

What do you want to achieve, because letting the client decide something is the worst choice you can make.

  1. Maybe pass the server time to the client. Calculate the difference and then start counting. But check on the server if the client is actually allowed to post.
  2. or let the server calculate how many seconds there are left until countdown reached and pass these to the client. But still server needs to check the valid time.

I would pick one of those. Let the server be the deciding factor and don't depend on the client. Since you can easily change PC time.

0
votes

When the timer on the client side reaches 0 send a api call to the server. When the server gets this api call puts a boolean to false. If this boolean is false, you ignore data. What I mean in code is something like this

var ignore = false;
app.post("/api/data/stop", function(req,res){
  ignore = true;
});
app.post("/api/data", function(req, res){
    if(!ignore){
  // listening to data
    }
});

app.listen(process.env.PORT, () => {
    console.log("server running on port: ", process.env.PORT);
});