javascript - Secure POST request from NodeJS to Node/Express hangs -


i'm using following send post data secure nodejs server:

file: 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 request: ' + e.message);     });      req.on('finish', function() {         console.log("finished request");     }); 

in otherwise functional expressjs server, these appropriate snippets:

file: app.js

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

file: 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 server (ubuntu), run main.js client (windows 7). req.write , req.end execute , "finished request" logs console, request callback never fires.

on server in app.js, app.post event fires , logs "received request" console. "saving config" never logs console. after kill (^c) main.js, express logs console "post /saveconfig".

i know i'm missing simple, i've read dozens of coding examples , can gather snippet, i've tried can find or think of. i'd guess request isn't finishing, don't know why. missing "exports.saveconfig" fire?

additional information

the answer posted below fixed problem. because i'm new stackoverflow, can't post own answer, here's rest of story...

i appreciate help. being still new javascript, found can learn lot object converting string. attempting convert req parameter string using custom function. discovered apparently running endless loop after using json.stringify instead.

the code looked 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 have thought above code should have logged following console- if serialize method in endless loop:

post /saveconfig

saving config

[nothing because of endless loop]

instead got:

saving config

connections property deprecated. use getconnections() method

being new javascript, assumed wrong request, server, or plumbing in-between. of course, debugging code added (along ignorance of js) compounded problem.

changing

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

to

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

and removing endless loop fixed problem.

the problem in you're app.js. use data.saveconfig inside callback without calling it. right way

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

or (i assume console.log debugging purposes):

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

you console.log() inside data.saveconfig method if want go second example.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -