title: Preventing duplicate POST requests in Node.js with Express
body: <p>I am getting undesired duplicate POST requests. The first time a POST is submitted after a reload, it is duplicated 6 times, for a total of 7 requests. all subsequent POSTs appear twice. this is how it appears in my console:</p>
  
  <pre><code>User  authenticated, redirecting to admin
  GET /admin 304 145ms
  GET /static/bootstrap/css/theme/flatly/bootstrap.css 304 1ms
  GET /static/bootstrap/css/jumbotron-narrow.css 304 1ms
  GET /static/jquery/jquery.js 304 0ms
  GET /static/bootstrap/js/bootstrap.min.js 304 0ms
  GET /static/images/hppLogo.png 304 0ms
  no value entered
  POST /remake 408 1ms - 16b
  no value entered
  POST /remake 408 1ms - 16b
  no value entered
  POST /remake 408 0ms - 16b
  no value entered
  POST /remake 408 1ms - 16b
  no value entered
  POST /remake 408 1ms - 16b
  no value entered
  POST /remake 408 1ms - 16b
  no value entered
  POST /remake 408 0ms - 16b
  no value entered
  POST /team-report 408 0ms - 16b
  no value entered
  POST /team-report 408 1ms - 16b
  no value entered
  POST /remake 408 0ms - 16b
  no value entered
  POST /remake 408 0ms - 16b
  </code></pre>
  
  <p>my html has this script in it:</p>
  
  <pre><code>script.
          $(document).ready(function () {
              $("#submit").click(function () {
                  document.getElementById("submit").disabled = true;
                  document.getElementById("submit").innerHTML = "Processing...";
                  $.post("/remake", $("#surveyForm").serialize(),
                          function (data) {
                              document.getElementById("submit").disabled = false;
                              document.getElementById("submit").innerHTML = "Generate";
                              document.getElementById("forResults").innerHTML = "Successfully created";
                              document.getElementById("resultsLink").innerHTML = 'samplelink.com' + data.redirect;
                              document.getElementById("resultsLink").href = data.redirect;
                          }
                  )
                          .error(function (xhr) {
                              switch (xhr.status) {
                                  case 409:
                                      document.getElementById("submit").disabled = false;
                                      document.getElementById("submit").innerHTML = "Generate";
                                      document.getElementById("forResults").innerHTML = "Entry does not exist";
                                      document.getElementById("resultsLink").innerHTML = '';
                                      break;
                                  case 408:
                                      document.getElementById("submit").disabled = false;
                                      document.getElementById("submit").innerHTML = "Generate";
                                      document.getElementById("forResults").innerHTML = "Please enter a value";
                                      document.getElementById("resultsLink").innerHTML = '';
                                      break;
                                  default:
                                      document.getElementById("submit").disabled = false;
                                      document.getElementById("submit").innerHTML = "Generate";
                                      document.getElementById("forResults").innerHTML = "Failed to create";
                                      document.getElementById("resultsLink").innerHTML = '';
                              }
                          });
              });
          });
  </code></pre>
  
  <p>the form button has <code>type="submit"</code>
  and the route calls this method:</p>
  
  <pre><code>var createTeamReport = function(teamName, res){
    "use strict";
    var params = {
        TableName: config.STARTUP_SIGNUP_TABLE,
        FilterExpression : 'company = :thisCompany',
        ExpressionAttributeValues: {
            ':thisCompany': {'S': teamName}
        }
    };
    if(teamName !== ''){
        db.scan(params, function(err, data) {
            if (err) {
                console.log('Error scanning DB: ');
                console.log(err);
                res.status(500).send('database error');
            }
            else if (Object.keys(data).length === 0) {
                res.status(409).send('no entries');
            }
            else {
                console.log("Database scan successful");
                res.send();
                jkteamreport.main(data, res);
            }
        });
    }
    else {
        console.log('no value entered');
        res.status(408).send('no value entered');
    }
  };
  </code></pre>
  
  <p>I am also using dynamoDB and AWS plugins for node. I know this is something stupidly simple wrong here and it's just driving me nuts that I can't figure it out</p>