(resumed from thread
https://groups.google.com/forum/#!topic/node-red/c_nPdKN1HzE)
I can't get a node-RED server to behave:
I get expected behaviour if I POST to a server running as a node.js script, at localhost:1881. If I POST the same message to a server running on node-RED at localhost:1880 it appears that the POST body does not arrive at the http in node. The server's http in node receives the POST, and the http out node returns a response, but the incoming and outgoing payloads seem empty objects.
What I am doing wrong? Could someone post a working example, to be copied?
Below are (a) the (working) node.js server and (b) the node-RED script that POSTs to either to the (working) node.js server or the (failing) node-RED server.
var http = require('http');
var server = http.createServer(function(req, res) {
if (req.method == 'POST') {
console.log("POST headers: " + JSON.stringify(req.headers));
req.on('data', function(chunk) {
var rsp = "Received POST body: " + chunk.toString();
console.log(rsp);
res.end(rsp);
})
}
});
server.listen(process.argv[2], function() {
// argv[2] is port number from command line
console.log("Listening on port: " + process.argv[2]);
});
[{"id":"f8ddeb2e.7fe338","type":"inject","name":"POST \"foo\" to \"/bar\" at port 1880 .","topic":"1880/bar","payload":"foo","repeat":"","crontab":"","once":false,"x":157.14285278320312,"y":328.5713839530945,"z":"4cf1daa3.af57a4","wires":[["dcc86858.e411f8"]]},{"id":"dcc86858.e411f8","type":"function","name":"Build URL and body","func":"// Send an HTTP POST.\n\n// use port 1880 if the server is a node-RED node\n// use port 1881 if the server is a node.js program\nvar portAndUrl = msg.topic;\nvar messageBody = msg.payload;\n\nmsg = {}; \n\nmsg.url = \"
http://localhost:\" + portAndUrl;\nmsg.payload = \"Payload is \" + messageBody;\n\nreturn msg;","outputs":1,"x":401.4285430908203,"y":354.2856912612915,"z":"4cf1daa3.af57a4","wires":[["ab4af96b.59387","692bcfaa.f629a"]]},{"id":"8b574262.92b8f","type":"debug","name":"HTTP POST Response","active":true,"complete":"false","x":808.5713729858398,"y":318.57137966156006,"z":"4cf1daa3.af57a4","wires":[]},{"id":"ab4af96b.59387","type":"http request","name":"HTTP POST","method":"POST","url":"","x":598.5714378356934,"y":317.1428470611572,"z":"4cf1daa3.af57a4","wires":[["8b574262.92b8f"]]},{"id":"692bcfaa.f629a","type":"debug","name":"HTTP POST request","active":true,"complete":"true","x":612.8571319580078,"y":385.71430110931396,"z":"4cf1daa3.af57a4","wires":[]},{"id":"8a5c14e2.24fe38","type":"http response","name":"/bar Server Out","x":719.9999465942383,"y":459.99997901916504,"z":"4cf1daa3.af57a4","wires":[]},{"id":"c122f975.6d0828","type":"debug","name":"Server POST request","active":true,"complete":"false","x":388.5714111328125,"y":540.0000247955322,"z":"4cf1daa3.af57a4","wires":[]},{"id":"2cdb04b5.83573c","type":"function","name":"/bar Server Processing","func":"// grab the POST body and return it\nvar POSTBody = msg.req.body;\nvar debugMsg = \"POST body: \" + JSON.stringify(POSTBody);\nconsole.log(debugMsg);\n\nmsg.res.payload = \"POST body received: \" + POSTBody;\n\nconsole.log(\"Returning \" + JSON.stringify(msg.res.payload));\n\nreturn [msg, debugMsg];","outputs":"2","x":417.14282989501953,"y":477.1428451538086,"z":"4cf1daa3.af57a4","wires":[["8a5c14e2.24fe38","4a3ae357.db5974"],["d4b8c37a.2cc58"]]},{"id":"4a3ae357.db5974","type":"debug","name":"Server POST Response","active":false,"complete":"false","x":728.5713882446289,"y":517.142813205719,"z":"4cf1daa3.af57a4","wires":[]},{"id":"62989b09.3fa3a4","type":"http in","name":"/bar server in","url":"/bar","method":"post","x":141.42857142857142,"y":499.99999999999994,"z":"4cf1daa3.af57a4","wires":[["2cdb04b5.83573c","c122f975.6d0828"]]},{"id":"d4b8c37a.2cc58","type":"debug","name":"ServerBody","active":false,"complete":"true","x":607.1428571428571,"y":580,"z":"4cf1daa3.af57a4","wires":[]},{"id":"d4488bb7.5539c8","type":"inject","name":"POST \"foo\" to \"/bar\" at port 1881 .","topic":"1881/bar","payload":"foo","repeat":"","crontab":"","once":false,"x":154.28570556640625,"y":382.857141494751,"z":"4cf1daa3.af57a4","wires":[["dcc86858.e411f8"]]}]