为什么我的POST请求每2分钟重复一次

时间:2018-01-06 18:32:56

标签: javascript html node.js express post

我的客户端正在发送1个POST请求,但由于某种原因,我的服务器每2分钟重复处理一次。它为什么这样做?

当我运行我的代码并从客户端单击提交按钮时,客户端控制台输出一次:

6/1/2018 @ 13:8:29 sendsettings request sent"

但服务器控制台将输出:

6/01/2018 @ 13:08:29 sendsettings request received
6/01/2018 @ 13:08:29: ---INTRUDER ALERT---
6/01/2018 @ 13:10:29 sendsettings request received
6/01/2018 @ 13:10:29: ---INTRUDER ALERT---
6/01/2018 @ 13:12:29 sendsettings request received
6/01/2018 @ 13:12:29: ---INTRUDER ALERT---
6/01/2018 @ 13:14:29 sendsettings request received
6/01/2018 @ 13:14:29: ---INTRUDER ALERT---

等...

以下是我的客户端代码的简化版本:

<!DOCTYPE html>
<html>
    <body>
        <div id="inputs">
            Password: <input type="password" id="password" name="password"> 
            <input type="submit" id="submit" value="Submit">
        </div>
        <script>
            document.getElementById("submit").onclick = submitInfoToServer;

            var xhttpPost = new XMLHttpRequest();

            function submitInfoToServer() {

                xhttpPost.open("POST", "/sendSettings", true);

                var password = document.getElementById("password").value

                var sendString = JSON.stringify({
                        password: password
                    });

                var currentdate = new Date(); 
                var datetime = currentdate.getDate() + "/"
                            + ((currentdate.getMonth()+1))  + "/" 
                            + (currentdate.getFullYear()) + " @ "  
                            + (currentdate.getHours()) + ":"  
                            + (currentdate.getMinutes()) + ":" 
                            + (currentdate.getSeconds());

                console.log(datetime + ' sendsettings request sent')
                xhttpPost.setRequestHeader("Content-Type", "application/json")
                xhttpPost.send(sendString);
            }
        </script>
    </body>
</html> 

简化的服务器代码是:

var express = require('express')
var app = express()
var https = require('https')
var json = require('express-json')
var bodyParser = require('body-parser')

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.set('port', (process.env.PORT || 5000))

app.post('/sendSettings', function(request, response) {

    var currentdate = new Date(); 
    var datetime = currentdate.getDate() + "/"
                + pad((currentdate.getMonth()+1))  + "/" 
                + pad(currentdate.getFullYear()) + " @ "  
                + pad(currentdate.getHours()) + ":"  
                + pad(currentdate.getMinutes()) + ":" 
                + pad(currentdate.getSeconds());

    console.log(datetime + ' sendsettings request received')

    if(request.body.password == 'password')
        console.log(datetime + ': correct password')
    else
        console.log(datetime + ': ---INTRUDER ALERT---')

})

1 个答案:

答案 0 :(得分:0)

一个老问题,但是如果有人想到:Node.js中请求的默认超时为2分钟。您不会从服务器发回任何响应。

修复服务器脚本,将其发送回去(例如:res.send('something')),这将停止发生。