简体   繁体   English

从NodeJS到Node / Express的安全POST请求挂起

[英]Secure POST request from NodeJS to Node/Express hangs

I'm using the following to send POST data to a secure nodejs server: 我正在使用以下内容将POST数据发送到安全的nodejs服务器:

File: main.js 档案:main.js

    var strdata = JSON.stringify({"data":"thisdata"});

    var options = {
        host: '192.168.1.63',
        port: 3001,
        path: '/saveconfig',
        method: 'POST',
        rejectUnauthorized: false,
        requestCert: true,
        agent: false,

        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(strdata)
        }   

    };


    var req = https.request(options, function(res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    console.log(req.write(strdata));
    console.log(req.end());

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    req.on('finish', function() {
        console.log("finished request");
    });

In an otherwise functional expressjs server, these are the appropriate snippets: 在其他功能正常的expressjs服务器中,以下是适当的片段:

File: app.js 文件:app.js

app.post('/saveconfig', function() { 
    data.saveconfig; console.log("received request"); } );

app.get('/getconfig', data.getconfig);

File: data.js 档案:data.js

exports.saveconfig = function(req, res) {
console.log("saveing config");

res.send(200);
res.end();

};

exports.getconfig = function(req, res) {
res.send("get OK");
}

With app.js running on the server (Ubuntu), I run main.js from the client (Windows 7). 在服务器(Ubuntu)上运行app.js之后,我从客户端(Windows 7)运行main.js。 req.write and req.end execute and "finished request" logs to the console, but the request callback never fires. req.write和req.end执行以及“完成的请求”日志记录到控制台,但是请求回调从不触发。

On the server in app.js, the app.post event fires and logs "received request" to the console. 在app.js中的服务器上,将触发app.post事件并将“接收到的请求”记录到控制台。 But "saving config" never logs to the console. 但是“保存配置”永远不会登录到控制台。 Only after I kill (^C) main.js, express then logs to the console "POST /saveconfig". 仅在杀死(^ C)main.js之后,express才登录到控制台“ POST / saveconfig”。

I know I'm missing something simple, but I've read dozens of coding examples and you can likely gather from my snippet, I've tried everything I can find or think of. 我知道我缺少一些简单的东西,但是我已经阅读了许多编码示例,并且您可能可以从我的摘录中收集到各种东西,我已经尝试了所有可以找到或想到的东西。 I'd guess the request isn't finishing, but I don't know why. 我猜请求没有完成,但是我不知道为什么。 What is missing to get "exports.saveconfig" to fire? 什么会导致“ exports.saveconfig”启动?

additional information 附加信息

The answer posted below fixed my problem. 下面发布的答案解决了我的问题。 Because I'm new to stackoverflow, I can't post my own answer, but here's the rest of the story... 因为我是Stackoverflow的新手,所以我无法发布自己的答案,但这是故事的其余部分...

I appreciate your help. 我感谢您的帮助。 Being still new to JavaScript, I found I can learn a lot about an object by converting it to string. 对JavaScript还是一个新手,我发现我可以通过将对象转换为字符串来了解很多有关对象的知识。 I was originally attempting to convert the req parameter to a string using a custom function . 我最初试图使用自定义函数将req参数转换为字符串。 I just discovered it was apparently running into an endless loop after using JSON.stringify instead. 我只是发现使用JSON.stringify代替之后显然陷入了一个无限循环。

The code looked something like this: 代码看起来像这样:

exports.saveconfig = function (db) {
    return function(req, res) {
        console.log("saving config");

        console.log(mymodule.serialize(req));

        res.end("OK");

        console.log(req.body);
   };
};

I would have thought the above code should have logged the following to the console- even if the serialize method was in an endless loop: 我本以为上面的代码应该将以下内容记录到控制台中,即使serialize方法处于无限循环中:

POST /saveconfig POST /保存配置

saving config 保存配置

[nothing because of the endless loop] [无尽的循环]

Instead I got: 相反,我得到了:

saving config 保存配置

connections property is deprecated. 连接属性已弃用。 Use getConnections() method 使用getConnections()方法

Being new to JavaScript, I assumed something was wrong with the request, the server, or some plumbing in-between. 作为JavaScript的新手,我认为请求,服务器或两者之间的管道有些问题。 Of course, then the debugging code I added (along with my ignorance of JS) compounded the problem. 当然,然后我添加的调试代码(以及对JS的无知)使问题更加复杂。

Changing 更改

app.post('/saveconfig', function() { 
data.saveconfig; console.log("received request"); } );

to

app.post('/saveconfig', datarts.saveconfig);

and removing the endless loop fixed the problem. 并消除无限循环可解决此问题。

The problem is in you're app.js. 问题出在你是app.js。 You use data.saveConfig inside your callback without calling it. 您可以在回调中使用data.saveConfig而不调用它。 The right way would be 正确的方法是

app.post('/saveconfig', function(req,res) { 
    data.saveconfig(req, res); 
    console.log("received request"); 
});

app.get('/getconfig', data.getconfig);

or (I assume the console.log is just for debugging purposes): 或(我假设console.log仅用于调试目的):

app.post('/saveconfig', data.saveconfig);

app.get('/getconfig', data.getconfig);

You could do your console.log() inside your data.saveconfig method if you want to go with the second example. 如果要使用第二个示例,则可以在data.saveconfig方法中执行console.log()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM