Composing APIs with Node-RED and JavaScript
Learn how to make APIs with JavaScrips and Node-RED in this awesome tutorial.
Join the DZone community and get the full member experience.
Join For Freeas the node-red website says “node-red is a tool for wiring together hardware devices, apis and online services in new and interesting ways”. it has been possible to use node-red for internet of things scenarios on bluemix for quite some time. with the new connect and compose (beta) service you can now also use node-red to compose complex apis via flow editor and javascript.
below is a simple example which is the same scenario that i implemented with the api management service. in order to reduce the number of requests from (mobile) clients two http requests are invoked before the results are returned to the client.
after you’ve created a service instance on bluemix you can choose to compose new apis. in the node-red editor the first thing you need is an http in node where you define the request type and url. the http in node is linked to a swagger-doc node where the input and output parameters are defined. to open the swagger-doc editor click on “edit” in the http in node.
the last node of the flow needs to be a http response node which returns the data in the variable msg.payload as defined in the swagger-doc node. everything else between the http in node (with the linked swagger-doc node) and the http response node is up to you. in my simple example i simply invoke two http get requests and put the responses together in one message.
here is the definition of the flow. i use several function nodes which contain javascript code. at some places the underlaying node.js implementation is exposed. for example msg.req.query can be used to read the parameter “name” in the incoming rest url request which is a function of the request object in express , the node.js web framework used in node-red.
[{
"id": "44eb9c6d.bb1464",
"type": "swagger-doc",
"summary": "hello world",
"description": "hello world",
"tags": "",
"consumes": "application/json",
"produces": "application/json",
"parameters": [{
"name": "name",
"in": "query",
"description": "name",
"required": false,
"type": "string"
}],
"responses": {
"200": {
"description": "success",
"schema": {
"properties": {
"java": {
"type": "string"
},
"node": {
"type": "string"
}
}
}
}
},
"deprecated": false
}, {
"id": "72b8b258.8d474c",
"type": "http in",
"name": "",
"url": "/hello",
"method": "post",
"swaggerdoc": "44eb9c6d.bb1464",
"x": 123.51248168945312,
"y": 59,
"z": "f4c8dbcb.0b3728",
"wires": [
["ca6abb2d.359548"]
]
}, {
"id": "5a5ae0c8.a5a52",
"type": "http response",
"name": "",
"x": 570.0124664306641,
"y": 325.1166687011719,
"z": "f4c8dbcb.0b3728",
"wires": []
}, {
"id": "1a4d6a88.e5b295",
"type": "http request",
"name": "node api",
"method": "get",
"ret": "txt",
"url": "",
"x": 340.8957977294922,
"y": 147.88888549804688,
"z": "f4c8dbcb.0b3728",
"wires": [
["a78a3c45.5875c"]
]
}, {
"id": "679c7fa2.98638",
"type": "debug",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 584.8957366943359,
"y": 382.888916015625,
"z": "f4c8dbcb.0b3728",
"wires": []
}, {
"id": "ca6abb2d.359548",
"type": "function",
"name": "setup node",
"func": "msg.url = 'http://node-swagger-docker-hello-world.mybluemix.net/hello?name=' + msg.req.query.name;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 181.89581298828125,
"y": 147.88888549804688,
"z": "f4c8dbcb.0b3728",
"wires": [
["1a4d6a88.e5b295"]
]
}, {
"id": "b520f29b.4adf1",
"type": "function",
"name": "setup java",
"func": "msg.url = 'http://gs-rest-service-cg.mybluemix.net/greeting?name=' + msg.req.query.name;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 180.89581298828125,
"y": 237.88888549804688,
"z": "f4c8dbcb.0b3728",
"wires": [
["afea2918.5015d8"]
]
}, {
"id": "afea2918.5015d8",
"type": "http request",
"name": "java api",
"method": "get",
"ret": "txt",
"url": "",
"x": 337.8957977294922,
"y": 237.88888549804688,
"z": "f4c8dbcb.0b3728",
"wires": [
["f794a650.086b58"]
]
}, {
"id": "a78a3c45.5875c",
"type": "function",
"name": "store node",
"func": "msg.nodemessage = json.parse(msg.payload);\nmsg.node = msg.nodemessage.message;\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 508.8697967529297,
"y": 148.36109924316406,
"z": "f4c8dbcb.0b3728",
"wires": [
["b520f29b.4adf1"]
]
}, {
"id": "79819a90.867e64",
"type": "function",
"name": "response",
"func": "msg.payload = {\n java: msg.java,\n node: msg.node\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 357.01246643066406,
"y": 356.1166687011719,
"z": "f4c8dbcb.0b3728",
"wires": [
["5a5ae0c8.a5a52", "679c7fa2.98638"]
]
}, {
"id": "f794a650.086b58",
"type": "function",
"name": "store java",
"func": "msg.javamessage = json.parse(msg.payload);\nmsg.java = msg.javamessage.content;\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 501.89576721191406,
"y": 237.88888549804688,
"z": "f4c8dbcb.0b3728",
"wires": [
["79819a90.867e64"]
]
}]
after you’ve composed the api, the connect and compose service provides a swagger definition of the api and an api explorer. to allow only certain applications to invoke apis, application keys and secrets are provided.
to learn more check out the connect and compose documentation and the email newsletter sample .
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments