Request for node-RED server example

2,248 views
Skip to first unread message

Charles Palmer

unread,
Feb 18, 2014, 4:56:23 PM2/18/14
to node...@googlegroups.com
(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"]]}]

Nicholas O'Leary

unread,
Feb 18, 2014, 5:21:45 PM2/18/14
to node...@googlegroups.com
Hi Charles,

there are a couple problems with your "/bar Server Processing" Function node:


1. You are creating debugMsg as a string. This is not a valid message object to return - it needs to be an object with a payload property. For example:

      var debugMsg = { payload: "POST body: " + JSON.stringify(POSTBody) };

This is why you don't see anything in the ServerBody debug node.

2. The HTTP Response node uses msg.payload as the body of the response. Your code is setting msg.res.payload instead, so is being ignored.


With those two things fixed, you flow works for me.

Nick



--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Charles Palmer

unread,
Feb 18, 2014, 6:58:51 PM2/18/14
to node...@googlegroups.com
Sorry Nick

With the /bar Server Processing node reduced to this:
var POSTBody = msg.req.body;
console.log(JSON.stringify(POSTBody));
msg.payload= "POST body received: " + POSTBody;
console.log("Returning " + JSON.stringify(msg.payload));
return msg;

var POSTBody = msg.req.body;
console.log(JSON.stringify(POSTBody));
msg.payload= "POST body received: " + POSTBody;
console.log("Returning " + JSON.stringify(msg.payload));
return msg;
var POSTBody = msg.req.body;
console.log(JSON.stringify(POSTBody));
msg.payload= "POST body received: " + POSTBody;
console.log("Returning " + JSON.stringify(msg.payload));
return msg;

the console.log output reads:

{}
Returning "POST body received: [object Object]"

Does this not say that the incoming POST body is missing? The node.js server at port 1881 does receive the expected POST body.
(I have not picked up your bug fix from yesterday. Is that the explanation?)
var POSTBody = msg.req.body;
console.log(JSON.stringify(POSTBody));
msg.payload= "POST body received: " + POSTBody;
console.log("Returning " + JSON.stringify(msg.payload));
return msg;

Nicholas O'Leary

unread,
Feb 19, 2014, 5:44:44 AM2/19/14
to node...@googlegroups.com
Hi Charles,

I don't see that. When I run the code you've pasted above in the function node, I get the following:

"Payload is foo"
Returning "POST body received: Payload is foo"

The bug fix I didn't yesterday shouldn't affect it, but you should grab the latest code just to be sure.

Here is the full updated flow:


[{"id":"8f9e79b.f706188","type":"inject","name":"POST \"foo\" to \"/bar\" at port 1880  .","topic":"1880/bar","payload":"foo","repeat":"","crontab":"","once":false,"x":196,"y":92.42853689193726,"z":"ab8bcf4.f54743","wires":[["e7c9aca5.18365"]]},{"id":"e7c9aca5.18365","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":440.2856903076172,"y":118.14284420013428,"z":"ab8bcf4.f54743","wires":[["46c7256f.b938dc","7cad1de4.8352e4"]]},{"id":"45e6e253.ba191c","type":"debug","name":"HTTP POST Response","active":true,"complete":"false","x":847.4285202026367,"y":82.42853260040283,"z":"ab8bcf4.f54743","wires":[]},{"id":"46c7256f.b938dc","type":"http request","name":"HTTP POST","method":"POST","url":"","x":637.4285850524902,"y":81,"z":"ab8bcf4.f54743","wires":[["45e6e253.ba191c"]]},{"id":"7cad1de4.8352e4","type":"debug","name":"HTTP POST request","active":true,"complete":"true","x":651.7142791748047,"y":149.57145404815674,"z":"ab8bcf4.f54743","wires":[]},{"id":"7d89d7a1.827628","type":"http response","name":"/bar Server Out","x":758.8570938110352,"y":223.8571319580078,"z":"ab8bcf4.f54743","wires":[]},{"id":"ae63be34.519c4","type":"debug","name":"Server POST request","active":true,"complete":"false","x":427.4285583496094,"y":303.857177734375,"z":"ab8bcf4.f54743","wires":[]},{"id":"7911f4c2.86ee0c","type":"function","name":"/bar Server Processing","func":"var POSTBody = msg.req.body;\nconsole.log(JSON.stringify(POSTBody));\nmsg.payload= \"POST body received: \" + POSTBody;\nconsole.log(\"Returning \" + JSON.stringify(msg.payload));\nreturn msg;","outputs":"2","x":455.9999771118164,"y":240.99999809265137,"z":"ab8bcf4.f54743","wires":[["7d89d7a1.827628","2dda52f7.d225ae"],["46c9d8b7.b93628"]]},{"id":"2dda52f7.d225ae","type":"debug","name":"Server POST Response","active":false,"complete":"false","x":767.4285354614258,"y":280.99996614456177,"z":"ab8bcf4.f54743","wires":[]},{"id":"ebb15d46.144ea","type":"http in","name":"/bar server in","url":"/bar","method":"post","x":180.2857186453683,"y":263.8571529388427,"z":"ab8bcf4.f54743","wires":[["7911f4c2.86ee0c","ae63be34.519c4"]]},{"id":"46c9d8b7.b93628","type":"debug","name":"ServerBody","active":false,"complete":"true","x":646.000004359654,"y":343.8571529388428,"z":"ab8bcf4.f54743","wires":[]},{"id":"e06f8fe1.1f907","type":"inject","name":"POST \"foo\" to \"/bar\" at port 1881  .","topic":"1881/bar","payload":"foo","repeat":"","crontab":"","once":false,"x":193.14285278320312,"y":146.71429443359375,"z":"ab8bcf4.f54743","wires":[["e7c9aca5.18365"]]}]




Reply all
Reply to author
Forward
0 new messages