clouddatabases-postgresql-helloworld-nodejs is a sample IBM Cloud application which shows you how to connect to an IBM Cloud Databases for PostgreSQL service to a IBM Cloud Foundry application written in Node.js.
-
If you do not already have an IBM Cloud account, sign up here
-
Download and install IBM Cloud CLI.
The IBM Cloud CLI tool tool is what you'll use to communicate with IBM Cloud from your terminal or command line.
-
Connect to IBM Cloud in the command line tool and follow the prompts to log in.
ibmcloud login
Note: If you have a federated user ID, use the
ibmcloud login --sso
command to log in with your single sign on ID. -
Create your database service.
The database can be created from the command line using the
ibmcloud resource service-instance-create
command. This takes a service instance name, a service name, plan name and location. For example, if we wished to create a database service named "example-psql" and we wanted it to be a "databases-for-postgresql" deployment on the standard plan running in the us-south region, the command would look like this:ibmcloud resource service-instance-create example-psql databases-for-postgresql standard us-south
Remember the database service instance name.
-
Make sure you are targeting the correct IBM Cloud Cloud Foundry org and space.
ibmcloud target --cf
Choose from the options provided.
-
Create a Cloud Foundry alias for the database service.
ibmcloud resource service-alias-create alias-name --instance-name instance-name
The alias name can be the same as the database service instance name. So, for our database created in step 4, we could do:
ibmcloud resource service-alias-create example-psql --instance-name example-psql
-
Clone the app to your local environment from your terminal using the following command:
git clone https://github.com/IBM-Cloud/clouddatabases-postgresql-helloworld-nodejs.git
-
cd
into this newly created directory. The code for connecting to the service, and reading from and updating the database can be found inserver.js
. See Code Structure and the code comments for information on the app's functions. There's also apublic
directory, which contains the html, style sheets and javascript for the web app. For now, the only file you need to update is the application manifest. -
Update the
manifest.yml
file.- Change the
name
value. The value you choose will be the name of the app as it appears in your IBM Cloud dashboard. - Change the
route
value to something unique. This will make be the base URL of your application. It should end with.mybluemix.net
. For exampleexample-helloworld-nodejs.mybluemix.net
.
Update the
service
value inmanifest.yml
to match the name of your database service instance name. - Change the
-
Push the app to IBM Cloud. When you push the app it will automatically be bound to the service.
ibmcloud cf push
Your application is now running at host you entered as the value for the route
in manifest.yml
.
The node-postgresql-helloworld app displays the contents of an examples database. To demonstrate that the app is connected to your service, add some words to the database. The words are displayed as you add them, with the most recently added words displayed first.
File | Description |
---|---|
server.js | Establishes a connection to the PostgreSQL database using credentials from VCAP_ENV and handles create and read operations on the database. |
main.js | Handles user input for a PUT command and parses the results of a GET command to output the contents of the PostgreSQL database. |
The app uses a PUT and a GET operation: