Inject o/p of the function node into html template using node red
This is my flow
[
{"id":"b9787755.ece25",
"type":"http in",
"z":"77c9615d.84a36",
"name":"httpin",
"url":"/rand",
"method":"get",
"upload":false,
"swaggerDoc":"",
"x":210,
"y":220,
"wires":[["413c2211.ae5254"]]
},
{
"id":"116ae20e.efc5a6",
"type":"template",
"z":"77c9615d.84a36",
"name":"",
"field":"payload",
"fieldType":"msg",
"format":"handlebars",
"syntax":"mustache",
"template":"Hello Random - {{ payload}}",
"x":540,
"y":260,
"wires":[["6e6cb7b7.a71f7"]]},
{"id":"6e6cb7b7.a71f7",
"type":"http response",
"z":"77c9615d.84a36",
"name":"http response",
"x":708,
"y":217.00003051757812,
"wires":[]
},
{
"id":"53ef12d8.fa353c",
"type":"inject",
"z":"77c9615d.84a36",
"name":"Inject1",
"topic":"Topic1",
"payload":"hello world",
"payloadType":"str",
"repeat":"2",
"crontab":"",
"once":true,
"onceDelay":"2",
"x":120,
"y":100,
"wires":[["413c2211.ae5254"]]
},
{
"id":"413c2211.ae5254",
"type":"function",
"z":"77c9615d.84a36",
"name":"Random Fn",
"func":"var context=\"\"\ncontext = \"hi\"+msg.payload.toString()+\"hi\"\nmsg.payload=context\nreturn msg\n",
"outputs":1,
"noerr":0,
"x":470,
"y":140,
"wires":[["5b901ff7.344e3",
"116ae20e.efc5a6"]]},{"id":"5b901ff7.344e3",
"type":"debug",
"z":"77c9615d.84a36",
"name":"Debug1",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"payload",
"x":679.0000610351562,
"y":154,
"wires":[]
}
]
So it sounds like you want to POST your sensor data to a node-red flow? Whenever you start a flow with an http in
node, you need to end that flow with an http response
(not http request
-- that is for calling out to another http endpoint). You also need to make sure you reuse the msg object and retain the req/res headers.
Since the output of your function is showing the correct string in the debug node, you just need to return that same string with the http response
node. In the example above, where you want to type the url http://servername/rand
into your browser, your http in
node would have to be configured for GET
requests. In this case, the output payload from the http in
node would be empty, so your function will not do what you expect. But if you configure it for POST
requests, then you will need to send your data programatically, not from a browser.
In the end, it sounds like you want two flows -- a POST
listener that takes the incoming sensor data and stores it in a database (or somewhere), and a GET
listener that takes the incoming req params, builds a SQL Select... statement, and returns the query results. For the POST
flow, even if you don't return any data, you still need to respond with a 200 OK code.