Skip to content

alr-staff/jovial-hand

Repository files navigation

CRUD - Node.js Example Application

Important
While you can run and interact with this example application on localhost, it requires that you also have a database installed and configured. This example application runs best when deployed on OpenShift with a PostgreSQL database. For more details on using this example application with a single-node OpenShift cluster, CI/CD deployments, as well as the rest of the runtime, see the Node.js Runtime Guide.
Important
This example application requires Node.js 8.x or greater and npm 5 or greater.
Important
As part of the process of creating this example application using developers.redhat.com/launch or the Fabric8 Launcher tool, set up a project with a CI/CD deployment of this example application. You can see the status of this deployment in your Single-node OpenShift Cluster or OpenShift Online Web Console.

Running the Example Application on a Single-node OpenShift Cluster

If you have a single-node OpenShift cluster, such as Minishift or Red Hat Container Development Kit, installed and running, you can also deploy your example application there. A single-node OpenShift cluster provides you with access to a cloud environment that is similar to a production environment.

To deploy your example application to a running single-node OpenShift cluster:

$ oc login -u developer -p developer

$ oc new-project MY_PROJECT_NAME

# Ensure that you use the following values for the user name, password and database name when creating your database application.
# The pre-configured values are used in the `credentials-secret.yml` and `deployment.yml` files in the `src/main/fabric8` directory of your example application project.

$ oc new-app -e POSTGRESQL_USER=luke -ePOSTGRESQL_PASSWORD=secret -ePOSTGRESQL_DATABASE=my_data openshift/postgresql-92-centos7 --name=my-database

# Wait for `my-database` application to be running.

$ npm install && npm run openshift

Interacting with the Example Application on a Single-node OpenShift Cluster

To interact with your example application while it is running on a Single-node OpenShift Cluster, you first need to obtain its URL:

$ oc get route MY_APP_NAME -o jsonpath={$.spec.host}

MY_APP_NAME-MY_PROJECT_NAME.LOCAL_OPENSHIFT_HOSTNAME

You can use the form at your application’s URL or you can use the curl command:

List all entries in the database
$ curl http://MY_APP_NAME-MY_PROJECT_NAME.LOCAL_OPENSHIFT_HOSTNAME/api/fruits

[ {
  "id" : 1,
  "name" : "Cherry",
}, {
  "id" : 2,
  "name" : "Apple",
}, {
  "id" : 3,
  "name" : "Banana",
} ]
Retrieve an entry with a specific ID
curl http://MY_APP_NAME-MY_PROJECT_NAME.LOCAL_OPENSHIFT_HOSTNAME/api/fruits/3

{
  "id" : 3,
  "name" : "Banana",
}
Create a new entry:
curl -H "Content-Type: application/json" -X POST -d '{"name":"pear"}'  http://MY_APP_NAME-MY_PROJECT_NAME.LOCAL_OPENSHIFT_HOSTNAME/api/fruits

{
  "id" : 4,
  "name" : "pear",
}
Update an Entry
curl -H "Content-Type: application/json" -X PUT -d '{"name":"pineapple"}'  http://MY_APP_NAME-MY_PROJECT_NAME.LOCAL_OPENSHIFT_HOSTNAME/api/fruits/1

{
  "id" : 1,
  "name" : "pineapple",
}
Delete an Entry:
curl -X DELETE http://MY_APP_NAME-MY_PROJECT_NAME.LOCAL_OPENSHIFT_HOSTNAME/api/fruits/1
Note
If you receive an HTTP Error code 503 as a response after executing these commands, it means that the application is not ready yet.

More Information

You can learn more about this example application and rest of the Node.js runtime in the Node.js Runtime Guide.