Create cloud native Node.js applications
Cloud native technologies are built to scale and grow in public, private, and hybrid cloud environments. Because they are resilient and easy to manage, they’re becoming increasingly important in our quick-changing technology landscape.
But building cloud native applications can sometimes feel daunting. Knowing what technologies to include and how to provision them is a huge task to accomplish before you can even begin coding. According to the
To simplify building cloud native Node.js apps, we’ve created an open source Yeoman generator to help you create fully functional Express-based Node.js applications. The generators correspond and align directly with the modules outlined on the CloudNativeJS.io. CloudNativeJS is a community project that provides assets and tools to help you to build cloud native Node.js applications and deploy them to Docker and Kubernetes-based clouds.
Open source Yeoman generators simplify Node.js development
The open source Yeoman generator, generator-nodeserver, creates fully functional Express-based Node.js application projects that include:
- Docker files for release and development containers.
- Helm charts with substitution values for deployment to any Kubernetes environment.
- Monitoring endpoints:
- Kubernetes liveness probes.
- Zipkin for end-to-end request tracing to spot bottlenecks.
- Prometheus for visualizing performance problems across app components.
- App Metrics with flame graphs for drill down into individual Node.js processes.
Build your Node.js project
So, now that you know what the generator does, let’s get you set up using it. Follow the steps below to install and use generator-nodeserver to create an Express-based Node.js server project.
You can build/deploy these projects using standard tools like npm, Docker, and Helm, or you can use IBM Cloud Developer Tools (IDT) to simplify these tasks. The projects created by the nodeserver generator include npm scripts to help you install and use IDT — just look for the idt:* scripts in your package.json file.
Watch the videos on Quick Start for IBM Cloud to get a better idea of what IDT can do for you.
Install the Yeoman generator
- Install Yeoman using the following command:
npm install -g yo
- Install nodeserver using the following command:
npm install -g generator-nodeserver
Run yo nodeserver
When you run ‘yo nodeserver’, the generator prompts you for the following:
- Project name: Specify the project name. The default is the current directory name. This is a required value.
- OpenApi document: Specify the relative or absolute file name of a Swagger document to direct the project’s code generation. A route stub will be scaffolded and registered for each route defined in the Swagger document. This is an optional value.
- Choose IBM Cloud services: Use ‘Y’ or ‘N’ to specify whether you want to scaffold IBM Cloud service enablement into your project. If you specify ‘Y’, you can then select one or more IBM Cloud service from the list. Your options are alert, appid, cloudant, mongo, object storage, postgre, push, redis, watson conversation. For each service you select, configuration and access scaffolding code is generated. IBM Cloud service enablement is optional.
You can also create an application using the default options by running this command:
yo nodeserver --headless
which defaults to:
- Project name: the name of the current working directory
- OpenApi Document: false
- Choose IBM Cloud Services?: false
You can build and run the generated project locally across all operating systems (Windows, macOS, Linux etc).
Run the project
To run the project locally, follow these steps:
- Install dependencies with the following command:
npm install
- Start the server using hte following:
npm start
To run the project locally with containers, follow these steps. We use IBM Cloud Developer Tools, which offers a command-driven approach for creating, developing and deploying a project and running your app locally with containers:
- Install IDT with command:
npm run idt:install
- Build a docker image and install dependencies with command:
npm run idt:build
- Run the application with command:
npm run idt:run
Test and debug your app
Here are a few quick ways to test and debug your application locally:
- Run unit tests using the
npm run idt:test
command - Run your project in debug mode, so you can attach a debug client using the
npm run idt:debug
command - Collect application performance metrics during development by running your project locally then going to localhost:3000/appmetrics-dash
- Check the health of your application by going to localhost:3000/health
- Run your project with hot-reload:
idt shell run-dev
then go to localhost:3000
The generated projects include deployment configuration for the following environments, which are available on IBM Cloud:
- Kubernetes: the projects include a Helm chart for deployment to Kubernetes
- Cloud Foundry: the projects include a manifest for deployment to Cloud Foundry
- Delivery Pipeline: the projects include a toolchain and pipeline definition for CI/CD deployment to the IBM Cloud. Check out the Garage Method to learn more.
Deploy to IBM Cloud
To deploy to IBM Cloud, you first need to create a free account on IBM Cloud.
Then, follow these steps:
- Install IDT using this command:
npm run idt:install
- Log in to IBM Cloud by running the command
bx login or bx login
–sso if your organization has enabled single sign-on with IBM Cloud. - Deploy to Cloud Foundry.
- Add a unique host by adding “host:
” to the manifest.yml file. - Run the command bx target -o ORG -s SPACE to select the Org and Space for your application.
- Then deploy to IBM Cloud with the following command: npm run idt:deploy.
- Deploy to Kubernetes.
- Set your region by running:
bx cs region-set <region>
- Set your terminal context to your cluster by running:
bx cs cluster-config <clustername>
- Run the command displayed in your terminal in response to the last step to export your kubeconfig file.
- Deploy your application with this command:
npm run idt:deploy -- --target container
Next steps
Check out IBM Cloud App Service to generate more complex applications like a MEAN web application.
Conclusion
The nodeserver Yeoman generator does a great job creating cloud-native projects, complete with all the essential ingredients. IDT is a great companion tool to simplify building, testing, and deploying your dockerized apps. Together, they make it a cinch to build and deploy cloud native apps to any Kubernetes environment, especially the IBM Cloud.