DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. Composing APIs with Node-RED and JavaScript

Composing APIs with Node-RED and JavaScript

Learn how to make APIs with JavaScrips and Node-RED in this awesome tutorial.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Dec. 19, 15 · Tutorial
Like (2)
Save
Tweet
Share
11.26K Views

Join the DZone community and get the full member experience.

Join For Free

as 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.

composeapi2

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.

composeapi1

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.

composeapi3

to learn more check out the connect and compose documentation and the email newsletter sample .

Node-RED JavaScript

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • RabbitMQ vs. Memphis.dev
  • API Design Patterns Review
  • Asynchronous HTTP Requests With RxJava
  • Top 5 PHP REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: