0

I am using node.js restify.

I have a HTTP Post handler that looks something like this;

var api_post_records = function (app, url_path) {
    function post_handler(req, res, next) {
    //code runs here
    res.send("ok"); //have to run some response back, otherwise python http post requests will hang
    app.post(url_path, post_handler);
}

If I remove the res.send("ok"); line, it will cause python http post requests to hang. I don't know why. Hope someone can provide the answer. I have to send some dummy HTTP response so as not to hang the python requests. Although current code works, I would like to know what should a proper HTTP response be if the HTTP post works fine.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

1 Answers1

1

That's some pretty non-standard code for restify, unless I'm missing something. Also, this has nothing to do with Python. Any request should fail if you comment out the res.send() method as that is what sends the response. You are also forgetting to call next(). Also, why are you recursively registering post_handler within post_handler?

This is generally how your code might be structured:

var restify = require('restify');
var server = restify.createServer();

var post_handler = function(req, res, next){
    res.send('ok');
    next();
}

server.post(url_path, post_handler);

server.listen(8000);
HeadCode
  • 2,770
  • 1
  • 14
  • 26
  • I can send `res.send('ok');` or `res.send('XXX anything');`. What should a proper response look like? – guagay_wk Mar 26 '16 at 04:20
  • 1
    @user781486 A proper response is whatever you would like to return to your python code request. In other words, whatever information you were trying to get from your node app to your python app in the first place. "Proper" could mean a lot of things, from JSON to XML to text, etc. – HeadCode Mar 26 '16 at 04:54