Creating Your First Node.js App
Let's build our first Node.js app on TIBCO Cloud™ Integration. This part of the tutorial will walk through creating the Node.js from an existing API specification. You can either use a Swagger 2.0 API spec, and generate code with API Modeler, or use an OpenAPI 3.0 API spec and generate code with other tools or frameworks.
For more information on how to create an API specification, see Modeling an API. For more information on OpenAPI 3.0, see https://swagger.io/specification/.
The API
As with every programming language and pretty much every program, the first thing you create is an Hello World
example. This isn't going to be any different, so below is the Swagger 2.0 API we'll use to build. The API has one GET
method called greeting
and takes a parameter called name
.
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "HelloWorld",
"x-lastModified": "Jul 29, 2017 23:13PM PST"
},
"paths": {
"/greeting/{name}": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Success response",
"schema": {
"$ref": "#/definitions/GetResponseSchema"
},
"examples": {
"application/json": {
"message": "Hello World!"
}
}
}
},
"parameters": [
{
"name": "name",
"in": "path",
"description": "",
"required": true,
"type": "string",
"format": ""
}
]
}
}
},
"definitions": {
"GetResponseSchema": {
"type": "object",
"properties": {
"message": {
"type": "string",
"default": "Hello World!"
}
}
}
}
}
Generate the Code
To generate the code from the API specification, log on to the TIBCO Cloud™ Integration website. On the global navigation bar, click , and click the API Model & Mock capability. You are directed to the All API specs page. From there hover over the API specification you want to generate the code for, click on the menu and click Generate Node.js code. That will give you a ZIP archive with all the code and a manifest.json
file that is needed when you want to push the app to TIBCO Cloud Integration.
Update the Code
Unzip the ZIP archive and you'll see a collection of files (you can get the ZIP archive of this sample from the resources section). The structure of the folder will be very similar to below:
├───hello_world_nodejs_app_1501395218703
│ |───manifest.json
│ └───hello_world_nodejs_app_1501395218703
| |───.eslintignore
| |───.eslintrc
| |───.npmignore
| |───package.json
| |───README.md
| |───server.js
│ ├───config
│ │ └───swagger.json
│ ├───data
│ │ |───mockgen.js
│ │ └───greeting
│ │ └───{name}.js
│ ├───handlers
│ │ └───greeting
│ │ └───{name}.js
│ ├───tests
│ │ └───greeting
│ │ └───{name}.js
│ └───util
│ └───logger.js
We'll not go over all the files right now, but we do want to make a small change the to {name}.js
file in the data/greeting
folder. We'll change it in such a way that the name we pass as a parameter will be used in the response we get back. The generated code will be something like this:
Mockgen().responses({
path: '/greeting/{name}',
operation: 'get',
response: '200'
}, callback);
And we'll replace that with this:
var data = JSON.parse('{"responses": {"message": "Hello ' + req.params.name + '"}}');
callback(null,data)
Instead of a generated mock response, it will respond back with Hello
and the name you put in.