Deploy a NodeJS App on Cloud Foundry Pt. 2 — Services

Shedrack Akintayo
Cloud Foundry Foundation
7 min readSep 25, 2020
Cover Photo

Introduction

In the first part of this tutorial, I demonstrated how we can quickly deploy a single page NodeJS application on Cloud Foundry. Cloud Foundry also lets us connect our application to a database service to help us scale our application as efficiently as possible.

In this tutorial, we will be deploying a NodeJS application on Cloud Foundry, making a few changes to our deployed application to see how fast Cloud Foundry updates our application in the browser and finally, binding our application to a database service.

Services in Cloud Foundry

Cloud Foundry offers its users a marketplace, where users can choose from a collection of reserved resources on-demand for various purposes. These reserved resources are called services.

Services provide resources including databases on a shared or dedicated server, or accounts on a SaaS app.

For more information about services, you can check the official Cloud Foundry documentation.

Setting Up and Deploying Our Application

To get started, we’d have to clone our application from Github and install its dependencies on our machine. Run the following command to clone the application to your machine:

git clone https://github.com/hacktivist123/cloudfoundry-nodejs-app.git

After cloning the application from Github, you can install the application on your local machine by running:

yarn install

After installing the application, you can start the application on your local machine by running the following command:

DEBUG=myapp:* npm start

After installing and starting the application, you can push your application to any of Cloud Foundry’s certified providers or distributions.

Note: In this tutorial I’ll be using Anynines Public Paas as my preferred distribution.

Now we’ll need to log in. You can do that by running the following command:

cf login

You’ll be asked for the API endpoint of your preferred Cloud Foundry distribution and also your login details before you can be successfully logged in.

Now, after you log in, we need to push our application. You can do that by running the following command:

cf push <application name>

This command will automatically deploy our application on Cloud Foundry and also provide a link (route) where we can see our application hosted live.

Below is an image in our deployed application:

Updating Our Application and Seeing the Changes Reflect.

Now that we have pushed our application, if we navigate to the generated route our application will currently looks like this:

Now let’s make a few changes to our application and then redeploy it.

Locate the index.js file in the routes folder and add the following piece of code:

/* GET Test Page. */
router.get (‘/test’ function (req, res, next) {
res.render(‘test’, {title: ‘This is a Test Page’})
})

Next, locate the index.pug file inside the view folder and add the following piece of code:

a(href=”/test”) Test Page

After that, create a test.pug file inside the views folder and add the following piece of code:

extends layoutblock content  h1= title  .imageimg(src=”https://www.cloudfoundry.org/wp-content/uploads/2017/01/CFF_Logo_vertical_RGB.png", alt=”cloud foundry logo”)

What we have done is create a new endpoint called /test and created a page that will be displayed when we hit that endpoint.

Now let’s re-deploy our application and see if it gets updated. We can do so by running the following command:

cf push <application name>

After re-deploying our application, if we navigate to our deployed application route and click on the Test Page link at the bottom of the page, we should see the new page that we created displayed.

Here is a visual demonstration of the process:

Setting Up a Database Service

After deploying our application, we need to set up a database for the application so that we can read and write data to it. In order to do that, we need to see the available services we have on the Cloud Foundry marketplace. We can do that by running the following command:

cf marketplace

If you ran the above command, you should see the following displayed on your screen depending on your preferred Cloud Foundry distribution.

For the purpose of this tutorial, we’ll be using the mongodb database service. Now to create a mongodb database service, run the following command:

cf create-service a9s-mongodb34 mongodb-nano cf-db

Where:

a9s-mongodb34 = database service name

mongodb-nano = database service plan

cf-db = your preferred name for the database service

If you executed the above command, you should see the following displayed on your terminal:

Now that we executed the above command, it takes a while for the database service to be fully created. In order to check the status of the database service we created, we can do that by running the following command in our terminal:

cf service cf-db

The above command will give us full details of our database service like so:

Now that the status of the service we created is “create succeeded” we need to bind the database service to our application, so that our application can have access to the database we created. We can do that by running the following command in our terminal:

cf bind-service <application name> cf-db

After running the above command, this will be displayed in your terminal:

Next, we need to restage our application so that the application can be updated with the new database service. We can do that by running the following command:

cf restage <application name>

The above command will re-deploy our application with the new database service that we have created.

Now we can see all the services bound to the application by running the following command:

cf services <application name>

Here is an example of what is displayed when I run the above command on my terminal:

To be sure that our application actually sees the new service that was created for it, we need to search the application’s environmental variables for our database connection details. We can do that by running the command in our terminal:

cf env <application name>

After running the above command, you should see the following displayed in your terminal:

If you look closely at the JSON file generated by the command, you’d see the database uri, username and also password.

And that’s it! We have successfully created a database service for our application and bound the database service to it. In order to use the database service in our application, all you need to do is parse the data from the application environmental variables like so:

var vcap_services = JSON.parse(process.env.VCAP_SERVICES)

The above code will give you access to all the environmental variables you’ll need to read and write to the database service. Environmental variables like database username, password, hostname, etc. are all available.

Alternatively, if you want an easier way to grab the environmental variables, you can use the cfenv library. This library will provide functions to parse Cloud Foundry-provided environment variables, so you wouldn’t have to worry about that yourself and all you have to do is to install and initialize the library like so:

Installation:

npm install cfenv

Usage:

var cfenv = require(“cfenv”)var appEnv = cfenv.getAppEnv()

Conclusion

In this tutorial, we explored Services in Cloud Foundry, and we created and connected our application to a mongoDB database. We also updated our application locally and then redeployed to see our changes updated live and instantly.

If you have any questions or contributions, please do not hesitate to let us know in the comment section.

The supporting repo for this article can be found here.

Cloud Foundry Summit Europe 2020 is built by and for the Cloud Foundry community. Whether you’re new to Cloud Foundry, you’re a long-time contributor building the platform, or you’re using Cloud Foundry to attain your business goals, Cloud Foundry Summit is the place to collaborate with other developers, operators, CIOs and IT professionals to shape the future of the project, share best practices and innovate together.

Dates: Oct 21st & 22nd 2020

The best way to connect with the Cloud Foundry community is to join our Slack Workspace at https://slack.cloudfoundry.org/. Those in the Slack community help you get quickly connected with other members or someone from the Cloud Foundry Foundation.

--

--