permalink |
---|
/guides/collection-nodejs |
Note
|
This repository contains the guide documentation source. To view the guide in published form, view it on the website. |
You will learn how to build a simple containerized microservice application from a Kabanero Node.js Collection in your VS Code IDE.
There are 3 Kabanero Collections for Node.js; one that includes just the runtime environment (nodejs
) and others that include
the Express or LoopBack frameworks (nodejs-express
or nodejs-loopback
). The way in which you use these collections is
similar. This example uses the nodejs-express
Kabanero Collection, which contains all the components you need to develop
your microservice application.
-
You must have Docker installed.
-
You must install the VS Code Codewind extension from the VS Code Marketplace, which creates a CODEWIND workspace and downloads Docker images to your Docker registry.
-
(Optional) If you have an enterprise-specific Kabanero Collection Hub, you need the URL to your index file.
You are now ready to create your first project!
Kabanero brings together open source technologies that provide a framework for developing cloud native microservice applications. As a developer, you are likely to have a preferred IDE for writing application code. In this guide, we will show you how you can develop a simple containerized microservice application that is based on the Node.js™ Express Kabanero Collection in a VS Code IDE.
Kabanero contains Appsody, which is designed to enable applications to be built and tested inside a Docker container. To access Appsody functions from your IDE, you need Eclipse Codewind, which provides a rich user interface that integrates seamlessly into VS Code. Codewind also provides performance and monitoring tools to help you test and debug applications in containers.
When you install Codewind in VS Code, an extension to Appsody is automatically installed as part of your Codewind workspace. By default, Codewind is configured to use a set of standard Codewind templates for developing cloud native applications. Codewind also contains configuration information for templates in the public Kabanero Collection Hub, but this configuration is not enabled by default.
To check the repository configuration of your Codewind installation, open your Codewind workspace in the VS Code Explorer view
and locate the .config/repository_list.json
file. The content is similar to the following example:
In this example you can see that the repository for Standard Codewind Templates is enabled by default ("enabled": true,
), whereas
the public repository for Kabanero Collections (v0.1.2) is disabled by default ("enabled": false,
). You can modify this
setting to suit your requirements.
As a developer, your enterprise architect might provide you with the URL of a Kabanero Collection Hub that your organisation has defined for developers to use. In this case, you can update the URL string for the Kabanero Collections to point to this URL instead. If you haven’t been given a URL for your organisation, you should clone the public Kabanero Collection Hub locally (The public collection is updated on a regular basis, so by making a copy, you avoid adopting unplanned changes).
To keep things simple in this guide you will use the public Kabanero Collection Hub repository to choose your application template.
Enable the public Kabanero Collection Hub repository now, by changing the value for this repository in your .config/repository_list.json
to "enabled": true,
. For the change to take effect you must stop and restart Codewind. Hover over the CODEWIND extension and click on
the word On to toggle the status to Off. When the Codewind containers have stopped, click on Off to restart them.
Codewind is now ready to use the public Kabanero Collection Hub.
Find the CODEWIND folder in your VS Code Explorer view. The Projects (Local) folder should contain No projects. To create a new project, click on (Click here to create a project).
In this guide, we are going to create a nodejs-express
microservice application.
From the Create a new project window, choose the Appsody Node.js Express simple template, as shown in the following diagram:
Choose a suitable name for your project and press Enter to save it.
Your new project is created, built, and started inside a container, which is linked to the VS Code workspace.
Projects are created with a default set of files, which you can find in a project folder in your CODEWIND-WORKSPACE.
Click on app.js
to view the code for the "Hello from Appsody" app.
const app = require('express')()
app.get('/', (req, res) => {
res.send("Hello from Appsody!");
});
module.exports.app = app;
To see the running app, right-click on the project in the CODEWIND folder and click Open App.
Your browser opens to display "Hello from Appsody!" on http://127.0.0.1:<local_port_number>/
. Make a note
of the <local_port_number> for your next task.
You are now going to create a new route that listens on http://127.0.0.1:<local_port_number>/example
.
Create a new file called example.js
in your project folder and populate it with the following code:
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('NEW ROUTE LISTENING');
});
module.exports = router;
Save the changes.
Edit the app.js
file and update the contents to match the following code:
const app = require('express')()
var exampleRouter = require("./example")
app.get('/', (req, res) => {
res.send("Hello from Appsody!");
});
app.use("/example", exampleRouter);
module.exports.app = app;
Save the changes.
Codewind watches for file changes and automatically updates your application. Point your browser to
http://127.0.0.1:<local_port_number>/example
to see your new route, which displays NEW ROUTE LISTENING.
You can perform a number of operations through the VS Code interface that help you develop, test, and debug your application. Right-click on your project to see a list of available tasks:
-
you can disable the automated build of your project and build it on demand
-
you can restart your application in run mode or debug mode
-
you can view the available logs to troubleshoot issues
-
you can find information about the running app by opening the Project Overview. VS Code displays information about your project, including the location, status, and any ports in use. The output is similar to the following screenshot:
image::/img/guide/projectoverview.png[link="/img/guide/projectoverview.png" alt="Diagram shows the Project Overview pane, which provides information about the status of the app.""]
-
you can stop the application, by clicking the Disable project button.
At some stage in development, you might want to do some local performance testing. As well as checking whether your code runs cleanly, Codewind provides application metrics and performance monitoring. For more information about developing applications with Codewind for VS Code, see the Codewind documentation.
Congratulations! You have now learned the basic steps for developing a microservice application in VS Code that’s based on the Node.js Express Kabanero Collection.
When you’ve finished developing and testing your microservice application on your local system, the next stage in the process is to test the application on a kubernetes or Knative environment. Your role in the overall process might end by delivering your changes to a GitHub repository. Here, your operations team can automate the deployment of your microservice application to kubernetes or Knative by implementing Tekton webhooks that trigger Tekton pipelines.
Want to learn about Tekton? Using Tekton pipelines to deploy microservice applications is covered in different guide.