Time interval between GET and POST requests in Node / Express

I am building a quiz site using Node / Express.

How can I ensure that users submit the quiz within a specified time frame without using client-side javascript? That is, if the survey takes 10 minutes, the submission after that should be flagged as too late.

Both GET and POST requests refer to the same URL

+3


source to share


2 answers


The lack of security and poor connectivity you want, but what I would like to do is start a timer server when the quiz is served to a client, and reject it if a response is sent after a given period (10M plus a small margin).

And so that the client knows, you can encode a client timer that will use the value set directly in the client by the html server when serving the page as the start time.



Hope this helps!

+2


source


I had the same question to be resolved in the app to evaluate.

This is how I can store the new date so that I can check the server later if the time is greater than my limit when responding:

Client code:

startEvaluation: function (data) {
        return $http.put(startUrl + '/' + data._id,data);
      },

      

Server code:

exports.startEvaluation = function (req, res) {
  if(req.body.status==="NEW")req.body.status="STARTED";
  req.body.started=Date.now();
    Evaluation.findOneAndUpdate({_id: req.body._id}, req.body, {upsert:true , new: true }, function(err, evaluation)
    {
       if (err) {
            return handleError(res, err);
         }
         if (!evaluation) {
            return res.send(404);
         }
    });
};

      

Hope it helps

EDIT:



As you said they are the same urls, you can use a parameter (like new). This way, if you answer with a new condition, you simply call the begin method:

exports.answer = function (req, res) {
  if(req.body.status==="NEW")
  {
    req.body.status="STARTED";
    req.body.started=Date.now();
  }
  else
  {
    //Process answer here and compare another Date.now() with the stored date, and your time limit to answer
    console.log("answer");
  }

    Evaluation.findOneAndUpdate({_id: req.body._id}, req.body, {upsert:true , new: true }, function(err, evaluation)
    {
       if (err) {
            return handleError(res, err);
         }
         if (!evaluation) {
            return res.send(404);
         }
        //here is the updated evaluation with the started value.
        return res.json(200, evaluations);

    });
};

      

Client method:

 $scope.saveAnswer = function ()
            {

                if($rootScope.evaluation.status==="NEW")
                {
                    evaluationService.startEvaluation($rootScope.evaluation);
                }
                else
                {
                  //Your answer treatment
                } 

}

      

In any case, a little improvement on the server could be to check the validation test needs to be run to allow the response.

EDIT

Here is the code for the server that should monitor the rating / poll status from the other side. This allows you to mark as "outdated" grades that the user did not complete within the allowed 20 minutes after the start of the quiz. Note that a setTimeout

to leave the task on the server side, waiting for the maximum time configured (example 20 minutes)

function marcarNoTerminada(id, body) {      
         //later you can check the status to filter answers given after limit hour
         body.status="NOT FINISHED";
         Evaluation.findOneAndUpdate({_id: id}, body, {upsert:true , new: true }, function(err, evaluation)
    {
       console.log("Evaluacion sin finalizar");
    });
}

exports.startEvaluation = function (req, res) {
  if(req.body.status==="NEW")req.body.status="STARTED";
  req.body.started=Date.now();
  req.body.validEnd= new Date(req.body.started+(60000*20));

    var theTimer = setTimeout(function(){marcarNoTerminada(req.body._id, req.body);}, 60000*20); 
    Evaluation.findOneAndUpdate({_id: req.body._id}, req.body, {upsert:true , new: true }, function(err, evaluation)
    {
       if (err) {
            return handleError(res, err);
         }
         if (!evaluation) {
            return res.send(404);
         }

        return res.json(200, evaluation);
    });
};

      

+2


source







All Articles