0

Mesos and Marathon REST API via cURL — A Hello World Example

In this hello-world style blog post, we will learn how to create load balanced Docker services in an open source DC/OS & Mesos environment. For that, we will perform Mesos and Marathon REST API calls using simple cURL commands.

First, we will install the DCOS CLI, before we retrieve the API Token. After playing around with several GET commands for Mesos, IAM, and Marathon, we will create a load-balanced Docker service via the Marathon REST API. In the end, we will show how to check the healthiness of the Marathon service via API calls.

References

Step 1: Install DCOS CLI

On the bootstrap node or a master node of the DCOS cluster, start a centos container as follows:

docker run -it centos bash

On a GUI based machine, open a browser and head to <DCOS_MASTER_URL>. Log in to the DCOS UI:

Choose “Install CLI” and cut&paste the content into the centos container:

sudo echo hallo 2>/dev/null || alias sudo=$@
[ -d /usr/local/bin ] || sudo mkdir -p /usr/local/bin && 
curl https://downloads.dcos.io/binaries/cli/linux/x86-64/dcos-1.10/dcos -o dcos && 
sudo mv dcos /usr/local/bin && 
sudo chmod +x /usr/local/bin/dcos && 
dcos cluster setup http://94.130.187.229 && 
dcos

The first line will make sure that the sudo prefix is ignored. You should see something as follows:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14.0M  100 14.0M    0     0  11.8M      0  0:00:01  0:00:01 --:--:-- 11.8M
If your browser didn't open, please go to the following link:

    http://94.130.187.229/login?redirect_uri=urn:ietf:wg:oauth:2.0:oob

Enter OpenID Connect ID Token:

Step 2: Retrieve the OpenID Token

Head to the URL given in the output and log in again. DC/OS will present your Connect ID Token:

Click “Copy to Clipboard” and paste it to the open terminal after “Enter OpenID Connect ID Token:”. The output should look like follows:

Command line utility for the Mesosphere Datacenter Operating
System (DC/OS). The Mesosphere DC/OS is a distributed operating
system built around Apache Mesos. This utility provides tools
for easy management of a DC/OS installation.

Available DC/OS commands:

        auth            Authenticate to DC/OS cluster
        cluster         Manage your DC/OS clusters
        config          Manage the DC/OS configuration file
        experimental    Manage commands that are under development
        help            Display help information about DC/OS
        job             Deploy and manage jobs in DC/OS
        marathon        Deploy and manage applications to DC/OS
        node            View DC/OS node information
        package         Install and manage DC/OS software packages
        service         Manage DC/OS services
        task            Manage DC/OS tasks

Get detailed command description with 'dcos  --help'.

We can test the DCOS CLI by entering dcos node command, which should produce an output similar to the one that follows:

# dcos node
   HOSTNAME           IP                           ID                    TYPE
 195.201.17.1    195.201.17.1   f2966d51-12b2-43f4-8d7a-1e8fb39fe80d-S0  agent
195.201.27.175  195.201.27.175  311a96d6-b5fc-4939-b9ef-92a6d1e0ae1f-S0  agent
master.mesos.   94.130.187.229    311a96d6-b5fc-4939-b9ef-92a6d1e0ae1f   master (leader)

Step 3: Retrieve the API Token

The manual procedure above will set the API token automatically.

Note: the API token is valid for 5 days only and the API requires you to re-fresh it by re-performing the manual steps 1 and 2. An automatic way of API retrieval via IAM API requires the installation of the DC/OS Enterprise edition and is described here.

Verify that the token is set:

# dcos config show core.dcos_acs_token
eyJhb...

Note: the first time, I tried, I got an error message like follows:

Property 'core.dcos_acs_token' doesn't exist

I have resolved the issue by re-authenticating with a ‘dcos auth login’ a second time.

Step 4: Mesos and Marathon API Calls

Preparation: Install jq and less

We will use the jq program (JSON Queries) for prettifying JSON output and less for easier handling the output of the curl commands. Let us install those programs now:

yum install -y epel-release
yum install -y jq less

Step 4.1: A first Mesos REST API Call

Test the API with our first API call:

(container)# curl --header "Authorization: token=$(dcos config show core.dcos_acs_token)" \
    http://94.130.187.229/mesos/master/state.json | jq '.' | less
{
 "version": "1.4.2",
 "git_sha": "732c49e6e98ac720df3418d9d868a6dfe1b2c6b5",
 "build_date": "2017-12-22 12:23:23",
 "build_time": 1513945403,
 "build_user": "",
...

Here, we have piped the prettified output from jq into a less. To avoid a jq error (“Usage: jq …”), we have run jq with the standard “do nothing” filter ‘.’ as found on StackOverflow: How to use `jq` in a shell pipeline.

You will need to add your master’s IP address.

Step 4.2: A first Marathon REST API Call

The following command will show all Marathon services:

(container)# curl -X GET -H 'Content-Type: application/json' \
    -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
    http://94.130.187.229/service/marathon/v2/apps/ | jq '.' | less
{
  "apps": [
    {
      "id": "/marathon-lb",
      "acceptedResourceRoles": [
        "slave_public"
      ],
...

You can see that I already have installed a Marathon Load Balancer. In a fresh DC/OS installation, you might get an empty apps list.

Step 4.3: A first IAM REST API Call

Show all users of the system (the output will show up in an uglified version as a one-liner, though):

(container)# curl -X GET -H 'Content-Type: application/json' \
    -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
    http://94.130.187.229/acs/api/v1/users | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   144  100   144    0     0  15425      0 --:--:-- --:--:-- --:--:-- 16000
{
  "array": [
    {
      "uid": "yourfirstuser@email.com",
      "description": "yourfirstuser@email.com"
    }
  ]
}

As a minimum, you will see a single list entry: the user you have logged into DC/OS first time.

Step 5: Create a new Docker Service via Marathon REST API

Now let us now create a new Marathon Service. For that, we will

  • define a service template named app.json
  • send an HTTP PUT with the app.json in the body to create/update the service
  • review the results in the graphical user interface and
  • access the created service.

Step 5.0: Install the Marathon Load Balancer

As a prerequisite of the load-balanced service, we intend to deploy, we need to install the Marathon load balancer as follows:

dcos package install marathon-lb

Step 5.1: Define a Service Template (app.json)

Now let us now create a new Marathon Service. For that, we define a service template named app.json as follows:

{
  "id": "/mynamespace/nginx-hello-world-service",
  "backoffFactor": 1.15,
  "backoffSeconds": 1,
  "container": {
    "portMappings": [
      {
        "containerPort": 80,
        "hostPort": 0,
        "labels": {
          "VIP_0": "/mynamespace/nginx-hello-world-service:80"
        },
        "protocol": "tcp",
        "servicePort": 80,
        "name": "mynamespace-nginx"
      }
    ],
    "type": "DOCKER",
    "volumes": [],
    "docker": {
      "image": "nginxdemos/hello",
      "forcePullImage": false,
      "privileged": false,
      "parameters": []
    }
  },
  "cpus": 0.1,
  "disk": 0,
  "healthChecks": [
    {
      "gracePeriodSeconds": 15,
      "ignoreHttp1xx": false,
      "intervalSeconds": 3,
      "maxConsecutiveFailures": 2,
      "portIndex": 0,
      "timeoutSeconds": 2,
      "delaySeconds": 15,
      "protocol": "HTTP",
      "path": "/"
    }
  ],
  "instances": 3,
  "labels": {
    "HAPROXY_DEPLOYMENT_GROUP": "nginx-hostname",
    "HAPROXY_0_REDIRECT_TO_HTTPS": "false",
    "HAPROXY_GROUP": "external",
    "HAPROXY_DEPLOYMENT_ALT_PORT": "80",
    "HAPROXY_0_PATH": "/mynamespace/nginx",
    "HAPROXY_0_VHOST": "195.201.17.1"
  },
  "maxLaunchDelaySeconds": 3600,
  "mem": 100,
  "gpus": 0,
  "networks": [
    {
      "mode": "container/bridge"
    }
  ],
  "requirePorts": false,
  "upgradeStrategy": {
    "maximumOverCapacity": 1,
    "minimumHealthCapacity": 1
  },
  "killSelection": "YOUNGEST_FIRST",
  "unreachableStrategy": {
    "inactiveAfterSeconds": 0,
    "expungeAfterSeconds": 0
  },
  "fetch": [],
  "constraints": []
}

Note that you need to replace the HAPROXY_0_VHOST (in red) by the IP address that matches your network.

Step 5.2: Create a Service

We now can create our first marathon service (“app”) as follows:

# curl  -X PUT -H 'Content-Type: application/json' \
    -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
    -d '@app.json' \
    http://94.130.187.229/service/marathon/v2/apps/mynamespace/nginx-hello-world-service
{"version":"2018-03-27T17:32:20.243Z","deploymentId":"a1bab3a4-3cbf-40de-a67e-3a1c961d9ad9"}

Note that we have received an immediate response including the deployment ID. With that information, we later can periodically check, whether the deployment is finished by asking the API about the status of the deployment.

Step 5.3 (optional): Review the Service in the GUI

We can see that we have created a new namespace:

Within that namespace, we have created a new service:

And the service is running on three container instances, as we have defined it in the app.json file:

Step 5.4 (optional): Access the Service

The created service can be accessed via the public agent’s IP address and port 80 on the path /mynamespace/nginx:

When you reload the page several times, you will see that the load balancer is using a round-robin balancing strategy to distribute the load among the three containers.

Step 6: Print App Summary

Step 6.1: App Summary

Above, we have seen a list of containers of our service in the GUI. Now let us retrieve the same information via API:

# APP_INFO=$(curl  -X GET -H 'Content-Type: application/json' \
      -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \  http://94.130.187.229/service/marathon/v2/apps/mynamespace/nginx-hello-world-service)

the output is organized as follows:

# echo "$APP_INFO" | jq
{
  "app": {
    "id": "/mynamespace/nginx-hello-world-service",
    ...
    "container": {            <----- information about Docker image, volumes, port mappings etc.
      ...                     
    },
    "cpus": 0.1,              <----- information about resource reservation, health checks, number of instances
    ...
    "labels": {               <----- information about HAPROXY configuration
      "HAPROXY_DEPLOYMENT_GROUP": "nginx-hostname",
      ...
    },
    ...
    "tasksStaged": 0,
    "tasksRunning": 3,
    "tasksHealthy": 3,
    "tasksUnhealthy": 0,
    "deployments": [],
    "tasks": [
      {
        "ipAddresses": [
          {
            "ipAddress": "172.17.0.8",
            "protocol": "IPv4"
          }
        ],
        "stagedAt": "2018-03-27T18:14:53.139Z",
        "state": "TASK_RUNNING",
        "ports": [
          19891
        ],
        "startedAt": "2018-03-27T18:14:54.324Z",
        "version": "2018-03-27T18:14:53.082Z",
        "id": "mynamespace_nginx-hello-world-service.bf0082f9-31ea-11e8-833d-f24b754eb1a3",
        "appId": "/mynamespace/nginx-hello-world-service",
        "slaveId": "311a96d6-b5fc-4939-b9ef-92a6d1e0ae1f-S0",
        "host": "195.201.27.175",
        "healthCheckResults": [
          {
            "alive": true,
            "consecutiveFailures": 0,
            "firstSuccess": "2018-03-27T18:14:56.131Z",
            "lastFailure": null,
            "lastSuccess": "2018-04-01T15:03:29.708Z",
            "lastFailureCause": null,
            "instanceId": "mynamespace_nginx-hello-world-service.marathon-bf0082f9-31ea-11e8-833d-f24b754eb1a3"
          }
        ]
      },
      {
      ... <--------- second container ("task")
      },
      {
      ... <--------- third container ("task")
      }
    ]
  }
}

The output shows us information on the deployed service (“app”) like

  • app id,
  • Docker container information,
  • information on the resource reservations
  • labels that are used to configure the HA proxy load balancer
  • health information and information on the number of containers
  • information on each and every container (“task”).

As an example, we can go to the “tasks” section of the output and extract the information that we can reach the first container in the tasks list on http://195.201.27.175:19891 (“host” 195.201.27.175 and “port(s)” 19891):

Step 6.2: Check Service Health

We can check the service health of a service (“app”) by checking following conditions:

  1. There should be no active deployments:
### Example:
### "deployments": [],
###
$ [ "$(echo "$APP_INFO" | jq '.app.deployments')" == '[]' ] && echo "OK: no active deployments"
OK: no active deployments
  1. There should be no staged tasks:
### Example:
### "tasksStaged": 0,
###
$ [ "$(echo "$APP_INFO" | jq '.app.tasksStaged')" == '0' ] && echo "OK: no staged tasks"
OK: no staged tasks
  1. All running tasks should be healthy:
### Example:
### "tasksRunning": 3,
### "tasksHealthy": 3,
###
$ [ "$(echo "$APP_INFO" | jq '.app.tasksRunning')" == "$(echo "$APP_INFO" | jq '.app.tasksHealthy')" ] && echo "OK: all running tasks are healthy"
OK: all running tasks are healthy
  1. There should be no unhealthy tasks:
### Example:
### "tasksUnhealthy": 0,
###
$ [ "$(echo "$APP_INFO" | jq '.app.tasksUnhealthy')" == '0' ] && echo "OK: no unhealthy tasks"
OK: no unhealthy tasks

Combined, we can check for the app health as follows:

#!/bin/bash

MASTER=94.130.187.229

function checkAppHealth {
   APP_INFO=$1
   ERROR=''
   DEPLOYMENTS="$(echo "$APP_INFO" | jq '.app.deployments')"
   TASKS_STAGED="$(echo "$APP_INFO" | jq '.app.tasksStaged')"
   TASKS_RUNNING="$(echo "$APP_INFO" | jq '.app.tasksRunning')"
   TASKS_HEALTHY="$(echo "$APP_INFO" | jq '.app.tasksHealthy')"
   TASKS_UNHEALTHY="$(echo "$APP_INFO" | jq '.app.tasksUnhealthy')"
   [ "$DEPLOYMENTS" == '[]' ] || ERROR="Found active deployments for this app: $DEPLOYMENTS"
   [ "$ERROR" != "" ] && return 1
   [ "$TASKS_STAGED" == '0' ] || ERROR="Found $TASKS_STAGED staged tasks for this app"
   [ "$ERROR" != "" ] && return 1
   [ "$TASKS_RUNNING" == "$TASKS_HEALTHY" ] || ERROR="Not all running tasks ($TASKS_RUNNING) seem to be healthy ($TASKS_HEALTHY)"
   [ "$ERROR" != "" ] && return 1
   [ "$TASKS_UNHEALTHY" == '0' ] || ERROR='Found $TASKS_UNHEALTHY unhealthy tasks'
   [ "$ERROR" != "" ] && return 1
   return 0
}

APP_INFO="$(curl -X GET -H 'Content-Type: application/json' \
                 -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
                 http://${MASTER}/service/marathon/v2/apps/mynamespace/nginx-hello-world-service)"
ERROR="$(checkAppHealth $APP_INFO)"

if [ "$ERROR" == "" ]; then
   echo "Service status: healthy"
else
   echo "Service status: ERROR: $ERROR"
fi

Summary

In this blog post, we have learned how to

  • install the DCOS CLI
  • retrieve the REST API Token in an open source DC/OS environment
  • use the Marathon REST API to create a load-balanced Docker service that can be accessed from the Internet
  • check the service health of a Marathon service.

 

Appendix: Print List of Containers of a Marathon Service

Above, we have seen a list of containers of our service in the GUI. Now let us retrieve the same information via API.

# curl  -X GET -H 'Content-Type: application/json' \
      -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
      http://94.130.187.229/service/marathon/v2/apps/mynamespace/nginx-hello-world-service 2>/dev/null \
      | jq '.app.tasks'
[
  {
    "ipAddresses": [
      {
        "ipAddress": "172.17.0.8",
        "protocol": "IPv4"
      }
    ],
    "stagedAt": "2018-03-27T18:14:53.139Z",
    "state": "TASK_RUNNING",
    "ports": [
      19891
    ],
    "startedAt": "2018-03-27T18:14:54.324Z",
    "version": "2018-03-27T18:14:53.082Z",
    "id": "mynamespace_nginx-hello-world-service.bf0082f9-31ea-11e8-833d-f24b754eb1a3",
    "appId": "/mynamespace/nginx-hello-world-service",

 

# curl -X GET -H 'Content-Type: application/json' \
     -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
     http://94.130.187.229/service/marathon/v2/apps/mynamespace/nginx-hello-world-service\
     | jq '.app.tasksHealthy'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3531    0  3531    0     0   746k      0 --:--:-- --:--:-- --:--:--  862k
3

Here, we can see that there are 3 healthy “tasks”, i.e. Docker containers.

Appendix: Continuously print List of Marathon Deployments

With following while loop, we can print the list of deployments:

# while true; do 
  curl -X GET -H 'Content-Type: application/json' \
  -H "Authorization: token=$(dcos config show core.dcos_acs_token)" \
  http://94.130.187.229/service/marathon/v2/deployments; echo ""
  sleep 1; 
done

When we now go into the DC/OS GUI and restart the service, we will see something as follows:

[]
[]
[]
[]
[]
[{"id":"0252a58b-9ff5-4ed5-9169-90b3d2cd6ea0","version":"2018-03-27T18:14:53.082Z","affectedApps":["/mynamespace/nginx-hello-world-service"],"affectedPods":[],"steps":[{"actions":[{"action":"RestartApplication","app":"/mynamespace/nginx-hello-world-service"}]}],"currentActions":[{"action":"RestartApplication","app":"/mynamespace/nginx-hello-world-service","readinessCheckResults":[]}],"currentStep":1,"totalSteps":1}]
[{"id":"0252a58b-9ff5-4ed5-9169-90b3d2cd6ea0","version":"2018-03-27T18:14:53.082Z","affectedApps":["/mynamespace/nginx-hello-world-service"],"affectedPods":[],"steps":[{"actions":[{"action":"RestartApplication","app":"/mynamespace/nginx-hello-world-service"}]}],"currentActions":[{"action":"RestartApplication","app":"/mynamespace/nginx-hello-world-service","readinessCheckResults":[]}],"currentStep":1,"totalSteps":1}]
[]
[]

I.e., the list of deployments is empty most of the time, but when you restart the service, a deployment is running for longer than one second.

Appendix: Retrieve IP Address and Port of a specific Docker Container

We can use the following commands to retrieve the IP address and TCP port of a certain container (tested in a DC/OS Enterprise environment with a service account):

Step 1: Create a Configuration File DCOS_API.cfg

export DCOS_API_USER=MyDcosUserName
export DCOS_API_PASSWORD=MyDcosPassword
export DCOS_API_HOST=https://mesos-master.company.com
export PROXYOPTION="-x proxy:8080"

Step 2: Create DCOS_API

#!/bin/bash

source $0.cfg

MESOS_UID="$DCOS_API_USER"
MESOS_PWD="$DCOS_API_PASSWORD"
MESOS_MASTER=$DCOS_API_HOST
MARATHON_LOCATION=/base/url

if [ "$#" == "2" ]; then
  VERB=$1
  [ "$VERB" != "GET" ] && echo "Warning: only GET is supported currently. Using GET"
  RESOURCE=$2
else
  echo "usage: ´$0 GET resource"
  exit 1
fi

MARATHON_APP_ID=${MARATHON_LOCATION}/${APP_ID}

TOKEN=`curl $PROXYOPTION -s -k -D - -H 'Accept: application/json'  -H 'Content-Type: application/json' "$MESOS_MASTER/acs/api/v1/auth/login" --data "{\"password\":\"$MESOS_PWD\",\"uid\":\"$MESOS_UID\" }" | grep token | awk -F ":" '{print $2}' | awk -F "\"" '{print $2}'`


#                        -H 'Accept: application/json' \
curl $PROXYOPTION -s -k -H "Authorization: token=$TOKEN" \
                        -H 'Accept: text/plain' \
                        -H 'Content-Type: application/json' \
                        "$MESOS_MASTER/service/marathon/$RESOURCE"

Read the host + port of a single Container from the Marathon REST API

#!/bin/bash

MESOS_UID="$DCOS_API_USER"
MESOS_PWD="$DCOS_API_PASSWORD"
MESOS_MASTER=$DCOS_API_HOST
MARATHON_LOCATION=/base/url

if [ "$#" == "1" ]; then
  APP_ID=$1
else
  echo "APP_ID_MISSING"
  exit 1
fi

MARATHON_APP_ID=${MARATHON_LOCATION}/${APP_ID}

TOKEN=`curl $PROXYOPTION -s -k -D - -H 'Accept: application/json'  -H 'Content-Type: application/json' "$MESOS_MASTER/acs/api/v1/auth/login" --data "{\"password\":\"$MESOS_PWD\",\"uid\":\"$MESOS_UID\" }" | grep token | awk -F ":" '{print $2}' | awk -F "\"" '{print $2}'`

APPS=`curl $PROXYOPTION -s -H "Authorization: token=$TOKEN" -H 'Accept: application/json' -H 'Content-Type: application/json' -s -k "$MESOS_MASTER/service/marathon/v2/apps/$MARATHON_APP_ID"`

HOST=`echo "$APPS" | awk -F "\"tasks\"" '{print $2}' | awk -F "\"host\":\"" '{print $2}' | awk -F "\"" '{print $1}'`
PORT=`echo "$APPS" | awk -F "\"tasks\"" '{print $2}' | awk -F "\"ports\":" '{print $2}' | awk -F "\"" '{print $1}' | sed -e 's/\[\(.*\)\],/\1/g'`

[ "$HOST" == "" ] && HOST=NOT_FOUND
[ "$PORT" == "" ] && PORT=NOT_FOUND

echo "$HOST:$PORT"

if [ "$DEBUG" != "" ]; then
   echo "MESOS_MASTER=$MESOS_MASTER" >&2
   echo "DCOS_API_HOST=$DCOS_API_HOST" >&2
   echo "DCOS_API_USER=$DCOS_API_USER" >&2
   echo "TOKEN=$TOKEN" >&2
   echo "APPS=$APPS" >&2
fi

 

1

Getting Started with DC/OS on AWS

In the step-by-step tutorial Getting Started with DC/OS on Vagrant, we have learned how to install a MesosPhere DC/OS data center operating system locally. This time, we will install a DC/OS system on AWS Cloud: existing AWS CloudFormation templates will help us create a fully functional DC/OS data center with a Mesos master and five Mesos slaves within less than two hours. At the end, we will test the environment by starting a “Hello World” service based on Docker from DC/OS’ administration panel and accessing the application from the Internet.

MesoSphere DC/OS is a Data Center Operating System, which is built upon Apache Mesos and Mesosphere Marathon, an open source container orchestration platform. It has the target to hide the complexity of data centers when deploying applications.

AWS, Amazon Web Services is the leading provider offering Infrastructure as a Service and more.

 

Beware that running DC/OS on AWS does not come for free. I am still in the free tier period, so I had to pay only $0.48 for a test duration of less than 45 minutes (measured from the time I have created to the point in time I have terminated the stack). However, the induced cost might be higher in your case. Also, I had to pay a lot more, as the time of usage increased and some of the free usage limits were exceeded.

I recommend to check your current bill before and after the the test on the AWS Billing Home for the region US-West-2.

The guide has been tested for the region us-west-2 and us-east-2. However, it has worked only for us-west-2; probably because the correct image IDs are missing for us-east-2.

We are loosely following https://aws.amazon.com/blogs/apn/announcing-mesosphere-dcos-on-aws/, but we had to add correct some commands and add some instructions on user permissions.

See also

Prerequisites

Step 1: Configure your Credentials

You need to have entered your AWS Access Key and Secret on the ~/.aws/credentials file:

[default]
aws_access_key_id = XXXXXXX
aws_secret_access_key = KKKKKKKK

Step 2: Create an SSH Key for DC/OS

aws --region us-west-2 ec2 create-key-pair --key-name dcos-demo-key --output text --query KeyMaterial > dcos-demo-key_us-west-2.pem
cp dcos-demo-key_us-west-2.pem dcos-demo-key.pem
chmod 600 dcos-demo-key.pem

This will create an additional key pair on region us-west-2 (before, I had no key pair on this region; now it is one key):

Step 3: Find Cloud Formation Template URL

The official DCOS documentation v1.10 on AWS installation offers two options:

For our tests, we will choose the basic variant with one Mesos master and five Mesos slaves.

The corresponding CloudFormation Templates can be found on this page.

We copy the “Launch Stack” link for us-west-2 with Single Master and paste it here:

https://console.aws.amazon.com/cloudformation/home?region=us-west-2#/stacks/new?templateURL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/a5ecc9af5d9ca903f53fa16f6f0ebd597095652e/cloudformation/single-master.cloudformation.json

From the link, we can see that the template URL is as follows. On a Linux shell (e.g. GIT Bash on Windows), we define:

TEMPLATE_URL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/a5ecc9af5d9ca903f53fa16f6f0ebd597095652e/cloudformation/single-master.cloudformation.json

Step 4: Launch the CloudFormation Stack from AWS CLI

Step 4.1: First Attempt to launch the Stack

From our main instructions page, we find something like:

aws --region us-west-2 cloudformation create-stack --stack-name dcos-demo \
    --template-url ${TEMPLATE_URL} \
    --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="dcos-demo-key" \
    --capabilities CAPABILITY_IAM

Note that there were some errors in the instructions page: the line feed formatting was wrong and a comma was missing. This has been corrected above.

If your AWS CLI is using a user without CloudFormation permissions, you will receive the following error message:

A client error (AccessDenied) occurred when calling the CreateStack operation: User: arn:aws:iam::924855196031:user/secadmin is not authorized to perform: cloudformation:CreateStack on resource: arn:aws:cloudformation:us-east-2:924855196031:stack/dcos-demo/*

If you have not encountered this error, you can skip the next three substeps.

Step 4.2: Create Policy for CloudFormation Permissions

On the EC2 Dashboard of the AWS Console for us-west-2 (choose right region in the URL), choose

–> Services
–> IAM
–> Policies
–> Create Policy
–> Select Policy Generator
–> Choose Parameters:
Effect: Allow
AWS Service: AWS CloudFormation
Actions: All
Actions ARN: *

–> Add Statement
–> edit Name, e.g. “CloudFormation”

–> Create Policy

Step 4.3: Attach Policy to User

–> Users
–> Choose your user
–> Add Permission
–> Attach existing policies directly
–> check “CloudFormation”

–> Next Review

–> Add permissions

Step 4.4: Try again: Create Policy for CloudFormation Permissions

TEMPLATE_URL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/a5ecc9af5d9ca903f53fa16f6f0ebd597095652e/cloudformation/single-master.cloudformation.json
aws --region us-west-2 cloudformation create-stack --stack-name dcos-demo \
    --template-url ${TEMPLATE_URL} \
    --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="dcos-demo-key" \
    --capabilities CAPABILITY_IAM

This time we get following Response:

{
“StackId”: “arn:aws:cloudformation:us-west-2:924855196031:stack/dcos-demo/0c90e5c0-c716-11e7-9e0d-50d5ca2e7cd2”
}

After some minutes, we will see CREATE_COMPLETE in the AWS Console of US West 2:

On the EC2 Dashboard, we see:

After clicking the “8 Running Instances” link, we see:

The DC/OS is up and running!

If you see other errors like

  • API: s3:CreateBucket Access Denied
  • API: iam:CreateRole User: arn:aws:iam::924855196031:user/secadmin is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::924855196031:role/dcos-demo-SlaveRole-LP582D7P32GZ
  • The following resource(s) failed to create: [Vpc, ExhibitorS3Bucket, SlaveRole, DHCPOptions]. . Rollback requested by user.

then follow the instructions in Appendix A. Those are permissions issues.

Step 5 (recommended): Restrict Admin Access

The default is that the machines are open to the Internet world. I recommend to change the settings, so only you can access your systems.

On the EC2 Dashboard -> Security Groups, check out the security group with the description “Enable admin access to servers” and edit the source IP addresses:

Replace 0.0.0.0/0 (any) to “My IP” for all sources.

–> Save

Note, this step needs to be repeated any time your source IP address changes. See Step B6 of AWS Automation based on Vagrant — Part 2: Installation and Usage of the Vagrant AWS Plugin, if you are interested in an example that shows how to update the security rules to point to “My IP” per shell script based on AWS CLI.

TODO: find a better way to secure the admin interfaces, e.g. by adapting the CloudFoundation templates before starting the stack. This way, the admin interfaces are not open to the world from the beginning on.

Step 6: Access the DC/OS Admin Console

Now let us access our DC/OS Admin Console. For that, let us find the public DNS name of the master:

$ aws cloudformation describe-stacks --region us-west-2 | grep dcos-demo-ElasticL | awk -F '"' '{print $4}'
dcos-demo-ElasticL-XRZ8I3ZZ2BB2-549374334.us-west-2.elb.amazonaws.com

This is the DNS name we can connect to:

In my case, I have signed in with Google.

We reach a nice dashboard:

Step 7: Install DCOS CLI

The easiest way to automate application orchestration is to make use of the DCOS CLI. For that, click on your name and then “Install CLI” and follow the instructions. You will find some dcos command examples in my previous blog post on DC/OS.

I have followed the Windows instructions, i.e.

dcos cluster setup http://dcos-demo-elasticl-pu3fgu8047kg-271238338.us-west-2.elb.amazonaws.com

A browser window was started and I have logged into the browser session via Google. Then the token was offered:

I had to Copy and paste the token into the command line:

Enter OpenID Connect ID Token: eyJ0eXAiOiJKV1QiLCJ…

After that you should be able to see the dcos services:

dcos service
NAME              HOST     ACTIVE  TASKS  CPU    MEM    DISK  ID
marathon       10.0.5.242   True     4    2.75  1836.0  0.0   d456c8ce-f0e6-4c61-9974-94e3426f5fe8-0001
metronome      10.0.5.242   True     0    0.0    0.0    0.0   d456c8ce-f0e6-4c61-9974-94e3426f5fe8-0000

Marathon and Metronome are already running.

Step 8: Install Marathon LB

(dcos package describe --config marathon-lb)
dcos package install marathon-lb
By Deploying, you agree to the Terms and Conditions https://mesosphere.com/catalog-terms-conditions/#community-services
We recommend at least 2 CPUs and 1GiB of RAM for each Marathon-LB instance.

*NOTE*: For additional ```Enterprise Edition``` DC/OS instructions, see https://docs.mesosphere.com/administration/id-and-access-mgt/service-auth/mlb-auth/
Continue installing? [yes/no] yes
Installing Marathon app for package [marathon-lb] version [1.11.1]
Marathon-lb DC/OS Service has been successfully installed!
See https://github.com/mesosphere/marathon-lb for documentation.

After clicking on marathon-lb, we the details of the configuration of the marathon load balancer:

 

Step 9: Create a Hello World Application

Similar to the blog post, where we have installed DC/OS locally via Vagrant, let us create a hello world application. We choose a NginX application that is displaying some information on the source and destination IP addresses and ports seen from within the container. For that, let us click

–> Services

–> RUN A SERVICE

–> JSON Configuration

Cut and paste following text into the field:

{
   "id": "nginx-hello-world-service",
   "container": {
     "type": "DOCKER",
     "docker": {
       "image": "nginxdemos/hello",
       "network": "BRIDGE",
       "portMappings": [
         { "hostPort": 0, "containerPort": 80, "servicePort": 10007 }
       ]
     }
   },
   "instances": 3,
   "cpus": 0.1,
   "mem": 100,
   "healthChecks": [{
       "protocol": "HTTP",
       "path": "/",
       "portIndex": 0,
       "timeoutSeconds": 2,
       "gracePeriodSeconds": 15,
       "intervalSeconds": 3,
       "maxConsecutiveFailures": 2
   }],
   "labels":{
     "HAPROXY_DEPLOYMENT_GROUP":"nginx-hostname",
     "HAPROXY_DEPLOYMENT_ALT_PORT":"10007",
     "HAPROXY_GROUP":"external",
     "HAPROXY_0_REDIRECT_TO_HTTPS":"true",
     "HAPROXY_0_VHOST": "dcos-demo-PublicSl-1NSRAFIDG6VZS-267420313.us-west-2.elb.amazonaws.com"
   }
}

As HAPROXY_0_VHOST you need to use the public slave’s load balancer address you can retrieve via AWS CLI via:

$ aws cloudformation describe-stacks --region us-west-2 | grep dcos-demo-PublicSl | awk -F '"' '{print $4}' 
dcos-demo-PublicSl-1NSRAFIDG6VZS-267420313.us-west-2.elb.amazonaws.com

 

Now:

–> REVIEW & RUN

–> RUN SERVICE

You will see that the nginx-hello-world-service is being deployed:

After some seconds, the 3 containers are up&running:

 

After clicking on the name of the service, you will see the three containers:

Note that the column “UPDATED” will disappear, if the browser width is too low. If you have a small screen, you can scale the browser content with CTRL and Minus.

Step 10 (optional): Reach the service from inside

On an internal host, I can reach the NginX server via two ways:

Step 10.1: Access Application Container on a Private Slave

The following command will return the HTML code of the single container running on a private slave:

curl 10.0.2.9:14679 # SlaveServerGroup

Here, we have chosen the Endpoint address we can retrieve from the services details page:

Step 10.2: Access the Load Balancer Address

We can also contact the internal load balancer endpoint for the service. This has the advantage that the access is load balanced among the different containers we have started for the service.

curl 10.0.6.204:10007 # PublicSlaveServerGroup

Here we have combined the Public slave IP address with the HAPROXY port we have configured as a label:

In the next step, we will access the load balancer endpoint via the Internet.

Step 11: Connect to the Service via Internet

Step 11.1: Direct Connection to the Public Slave

The CloudFormation stack is configured in a way that allows reaching the public slave via the Internet on port 10007. This allows us to access the hello world application directly:

Step 11.2: Connection via AWS Load Balancer

Consider a case where we have more than one public slave. In those situations, it is better to access the service via AWS load balancer, which will distribute the load among the different public slave marathon load balancers (i.e. HAPROXY load balancers). In our case, we access the service on port 80: http://dcos-demo-PublicSl-1NSRAFIDG6VZS-267420313.us-west-2.elb.amazonaws.com

The load balancer address can be retrieved via

$ aws cloudformation describe-stacks --region us-west-2 | grep dcos-demo-PublicSl | awk -F '"' '{print $4}'
dcos-demo-PublicSl-1NSRAFIDG6VZS-267420313.us-west-2.elb.amazonaws.com

By pasting the return value into the browser, we are redirected to the corresponding https page:

After refreshing the page, we will see that we will get answers from the other two containers as well:

With that, we have learned how to create a service and access it from the Internet.

 

Step 12: Explore the Marathon Load Balancer

You can access the marathon load balancer by retrieving the public IP address of the public slave from the AWS console (EC2):

We then access the HA Proxy statistics page and configuration page by entering the public IP address or DNS name into the URL field, and adding one of the following strings:

  • :9090/haproxy?stats
  • :9090/_haproxy_getconfig

Step13: Delete the Stack

Do not forget to delete the stack, since it will induce quite a bit of cost if you fail to do so. The stack can be deleted via AWS CLI as follows:

aws --region us-west-2 cloudformation delete-stack --stack-name dcos-demo

Better you check on the  AWS Console that all resources have been deleted successfully:

Summary

In this blog post, we have learned to install a DC/OS Cluster on AWS using an existing CloudFormation template. For that, we have used AWS CLI to spin up a DC/OS environment with a single master, a single public slave, and five private slaves (see Appendix ?? below how to tweak the template to run only two private slaves in order to save some money).

Similar to the tests we had performed on a local machine using Vagrant described in the post Getting Started with DC/OS on Vagrant, we have installed a marathon load balancer, before we have deployed a three-container hello-world application. We have shown how to access this application from the public Internet using the AWS elastic load balancer that has been installed automatically via the CloudFormation stack. Moreover, we have shown how to access the marathon load balancer’s statistics and configuration page.

In the course of this step by step tutorial, we have mastered

  • user permission challenges (see step 4 and Appendix A)
  • networking challenges

We had to figure out that the services are only reachable via the AWS load balancers.

Appendix A: Add required User Permissions

Appendix A1: Remedy S3 Permission Error

Symptoms


If your user lacks the correct S3 permissions, we will get following errors in the  AWS Console, when trying to start the CloudFormation stack:

  • API: s3:CreateBucket Access Denied
  • API: iam:CreateRole User: arn:aws:iam::924855196031:user/secadmin is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::924855196031:role/dcos-demo-SlaveRole-LP582D7P32GZ
  • The following resource(s) failed to create: [Vpc, ExhibitorS3Bucket, SlaveRole, DHCPOptions]. . Rollback requested by user.

Resolution

  1. Add S3 Permissions

2) Add IAM Policy:

Add Permissions -> Create policy

-> Policy Generator -> Select ->

-> Add Statement -> Next Step -> Edit Name “IAM” -> Create Policy

-> Filter: Policy Type: Custom managed

-> Choose “IAM”

Let us delete it via console and try again:

TEMPLATE_URL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/14509fe1e7899f439527fb39867194c7a425c771/cloudformation/single-master.cloudformation.json
aws --region us-west-2 cloudformation create-stack --stack-name dcos-demo \
    --template-url ${TEMPLATE_URL} \
    --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="dcos-demo-key" \
    --capabilities CAPABILITY_IAM

Now we get following success messages on the AWS console:

After some minutes in the EC2 console:

 

 

Appendix B: [AcceptEULA] do not exist in the template

TEMPLATE_URL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/14509fe1e7899f439527fb39867194c7a425c771/cloudformation/single-master.cloudformation.json
aws --region us-east-2 cloudformation create-stack --stack-name dcos-demo \
    --template-url ${TEMPLATE_URL} \
    --parameters ParameterKey=AcceptEULA,ParameterValue="Yes" ParameterKey=KeyName,ParameterValue="dcos-demo-key" \
    --capabilities CAPABILITY_IAM

This time we get:

A client error (ValidationError) occurred when calling the CreateStack operation: Parameters: [AcceptEULA] do not exist in the template

This StackOverflow Q&A has pointed to the right direction: I tried to wrap all parameters in ”, but then I got a syntax error, that a comma is expected. The correct syntax turned out to be:

TEMPLATE_URL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/14509fe1e7899f439527fb39867194c7a425c771/cloudformation/single-master.cloudformation.json
aws --region us-east-2 cloudformation create-stack --stack-name dcos-demo \
    --template-url ${TEMPLATE_URL} \
    --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="dcos-demo-key" \
    --capabilities CAPABILITY_IAM

with commata between all parameters.

Appendix C: “Template error: Unable to get mapping for NATAmi::us-east-2::default”

How to Reproduce:

Get Key for region=us-east-2 from here: copy the link address of the corresponding Launch Stack Link and paste it somewhere:

https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?templateURL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/a5ecc9af5d9ca903f53fa16f6f0ebd597095652e/cloudformation/single-master.cloudformation.json

TEMPLATE_URL=https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/a5ecc9af5d9ca903f53fa16f6f0ebd597095652e/cloudformation/single-master.cloudformation.json;

Create a key for US East:

aws --region us-east-2 ec2 create-key-pair --key-name dcos-demo-key --output text --query KeyMaterial > dcos-demo-key_us-east-2.pem;
cp -i dcos-demo-key_us-east-2.pem dcos-demo-key.pem;
chmod 600 dcos-demo-key.pem;

Try starting the Stack:

aws --region us-east-2 cloudformation create-stack --stack-name dcos-demo \
    --template-url ${TEMPLATE_URL} \
    --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="dcos-demo-key" \
    --capabilities CAPABILITY_IAM;

If the user has all needed permissions (see steps 4.x above), then we get the following error:

A client error (ValidationError) occurred when calling the CreateStack operation: Template error: Unable to get mapping for NATAmi::us-east-2::default

Workaround

I have not investigated this issue. However, I guess that the error has to do with missing mappings for the images (AMI). A workaround is to use region=us-west-2 instead of us-east-2.

Appendix D: ERROR: “parameter value decos-demo-key for parameter name KeyName does not exist”

Reproduce

If you closely follow the instructions on https://aws.amazon.com/blogs/apn/announcing-mesosphere-dcos-on-aws/, correct the syntax errors in the aws commands, but keep the wrong key name “decos-demo-key” instead of “dcos-demo-key”, you will encounter the following problem:

After creation of the stack, we ask for the status:

aws --region us-west-2 cloudformation describe-stacks --stack-name dcos-demo --query Stacks[0].StackStatus

You will get the response:

"ROLLBACK_COMPLETE"

On the AWS Console of US West 2 we get:

The following error message is displayed:

Parameter validation failed: parameter value decos-demo-key for parameter name KeyName does not exist. Rollback requested by user.

Solution:

Correct the demo key name: “dcos-demo-key” instead of “decos-demo-key”

Appendix E: Adapt the CloudFormation Template to your Needs

The CloudFormation template is spinning up one master, one public slave, a NAT machine and five (!) private slaves. For the purpose of hello world testing we are performing, two instead of five private slaves are plenty. For that, I have adapted the CloudFormation template as follows:

Step E.1: Download CloudFormation Template

curl https://s3-us-west-2.amazonaws.com/downloads.dcos.io/dcos/EarlyAccess/commit/a5ecc9af5d9ca903f53fa16f6f0ebd597095652e/cloudformation/single-master.cloudformation.json

Step E.2 Adapt CloudFormation Template

I have added following parameter to the template (in blue):

        "SlaveInstanceCount": {
            "Description": "Required: Specify the number of private agent nodes or accept the default.",
            "Default": "5",
            "Type": "Number"
        },
        "SlaveInstanceCountDesired": {
            "Description": "Required: Specify the number of private agent nodes or accept the default.",
            "Default": "2",
            "Type": "Number"
        },
        "PublicSlaveInstanceCount": {
            "Description": "Required: Specify the number of public agent nodes or accept the default.",
            "Default": "1",
            "Type": "Number"
        },

The default of this parameter is two instead of five.

In the same template, I have changed following parts (in blue)

        "SlaveServerGroup": {
            "CreationPolicy": {
                "ResourceSignal": {
                    "Timeout": {
                        "Fn::FindInMap": [
                            "Parameters",
                            "StackCreationTimeout",
                            "default"
                        ]
                    },
                    "Count": {
                        "Ref": "SlaveInstanceCountDesired"
                    }
                }
            },
            "Properties": {
                "MaxSize": {
                    "Ref": "SlaveInstanceCount"
                },
                "DesiredCapacity": {
                    "Ref": "SlaveInstanceCountDesired"
                },
                "MinSize": {
                    "Ref": "SlaveInstanceCountDesired"
                },

Note that the stack will be stuck in CREATE_IN_PROGRESS if the first Count is not changed from SlaveInstanceCount to SlaveInstanceCountDesired.

Step E.3: Create S3 Bucket

The template is too large to use it directly per file: you will get following error if you try to use the template as file TEMPLATE_FILE=template-file-name:

aws --region us-west-2 cloudformation create-stack --stack-name dcos-demo \
 --template-body ${TEMPLATE_FILE} \
 --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="AWS_SSH_Key" \
 --capabilities CAPABILITY_IAM
An error occurred (ValidationError) when calling the CreateStack operation: 1 validation error detected: Value '<the json cloudformation template is printed>' at 'templateBody' failed to satisfy constraint: Member must have length less than or equal to 51200

The solution is to move the template to an S3 bucket in the same region. Now let us create the bucket:

aws s3api create-bucket --bucket my-us-west-2-bucket --region us-west-2

Step E.4: Copy Template to S3 Bucket

The template file can be copied to the S3 bucket via a command like:

aws s3 cp template_filename s3://my-us-west-2-bucket/

Step E.5: Use Template

Now we are ready to use the S3 bucket URL to create the stack:

TEMPLATE_URL='https://s3.amazonaws.com/my-us-west-2-bucket/template_filename'
SSH_KEY=dcos-demo-key
   aws --region us-west-2 cloudformation create-stack --stack-name dcos-demo \
       --template-url ${TEMPLATE_URL} \
       --parameters ParameterKey=AcceptEULA,ParameterValue="Yes",ParameterKey=KeyName,ParameterValue="AWS_SSH_Key" \
       --capabilities CAPABILITY_IAM

After 15 minutes or so, you should see that the stack is up and running with two private slave instances:

Appendix F: Configuration

F.1 Master cloud-config.yml

Is found on /usr/share/oem/cloud-config.yml:

#cloud-config

coreos:
  units:
    - name: etcd.service
      runtime: true
      drop-ins:
        - name: 10-oem.conf
          content: |
            [Service]
            Environment=ETCD_PEER_ELECTION_TIMEOUT=1200

    - name: etcd2.service
      runtime: true
      drop-ins:
        - name: 10-oem.conf
          content: |
            [Service]
            Environment=ETCD_ELECTION_TIMEOUT=1200

    - name: user-configdrive.service
      mask: yes

    - name: user-configvirtfs.service
      mask: yes

    - name: oem-cloudinit.service
      command: restart
      runtime: yes
      content: |
        [Unit]
        Description=Cloudinit from EC2-style metadata

        [Service]
        Type=oneshot
        ExecStart=/usr/bin/coreos-cloudinit --oem=ec2-compat

  oem:
    id: ami
    name: Amazon EC2
    version-id: 0.0.7
    home-url: http://aws.amazon.com/ec2/
    bug-report-url: https://github.com/coreos/bugs/issues

F.2 Public Slave cloud-config.yml

#cloud-config

coreos:
  units:
    - name: etcd.service
      runtime: true
      drop-ins:
        - name: 10-oem.conf
          content: |
            [Service]
            Environment=ETCD_PEER_ELECTION_TIMEOUT=1200

    - name: etcd2.service
      runtime: true
      drop-ins:
        - name: 10-oem.conf
          content: |
            [Service]
            Environment=ETCD_ELECTION_TIMEOUT=1200

    - name: user-configdrive.service
      mask: yes

    - name: user-configvirtfs.service
      mask: yes

    - name: oem-cloudinit.service
      command: restart
      runtime: yes
      content: |
        [Unit]
        Description=Cloudinit from EC2-style metadata

        [Service]
        Type=oneshot
        ExecStart=/usr/bin/coreos-cloudinit --oem=ec2-compat

  oem:
    id: ami
    name: Amazon EC2
    version-id: 0.0.7
    home-url: http://aws.amazon.com/ec2/
    bug-report-url: https://github.com/coreos/bugs/issues

References

 

2

Angular Universal CLI – Step-by-Step Example with REST Client

This time we will learn how to create a small Angular Universal CLI project that is using the WordPress REST service to retrieve and display the title and content of a WordPress blog post.

Angular Universal CLI combines the Universal features like server-side rendering (see Angular 4 Universal: Boosting Performance through Server Side Rendering) with a state-of-the-art handling of Angular projects by Angular CLI.

On a previous blog post, I have given an introduction to server-side rendering via Angular Universal. There, we had cloned a Universal seed file and added a REST client that has retrieved and displayed the content of a WordPress blog post. Later, I have found out that I have used a seed project that does not support Angular CLI. Angular CLI is the more modern way of handling Angular projects. In this blog post, we learn how to port (or create) the end-to-end tests and feature code to a seed project that has been created with Angular Universal CLI. With that, we have access to all ng commands provided by Angular CLI.

Step 0: Get Access to a Docker Host

The instructions will work on any Docker host with 2 GB available RAM. If you do not have access to a Docker host yet, I recommend following the step 0 instructions on my JHipster post.

Step 1: Create Aliases for often used Commands

In this tutorial, we will use following pre-defined aliases and functions for often used commands:

# functions
cli() {
 docker run -it --rm -w /app -v $(pwd):/app --net=host oveits/angular-cli:1.4.3 $@
}
npm() {
 cli npm $@
  if [[ "$@" == "i" ]] || [[ "$@" == "install" ]] ; then
    sudo chown -R $(whoami) .
  fi
}

# aliases
alias ng='cli ng $@'
alias protractor='docker run -it --privileged --rm --net=host -v /dev/shm:/dev/shm -v $(pwd):/protractor webnicer/protractor-headless $@'
alias own='sudo chown -R $(whoami) .'

Step 2: Fork & Clone Universal Starter from GIT

The Github project /universal-starter is a seed project that has been created with Angular CLI. Fork the project and clone it to your local machine (use your own Github name instead of mine, oveits):

$ git clone https://github.com/oveits/universal-starter
$ cd universal-starter

Step 3: Create e2e Test

The universal starter comes with no e2e tests. Let us change that now.

Step 3.1: Add e2e Folder from your existing project

If you have already created specs in an existing project, then copy them into a new e2e folder our project

cd project folder
mkdir e2e
cp <whereever you have your existing specs> e2e/

In my case, I intend to add the functionality I have developed on my blog post Behavior-Driven Angular – Part 2: Inserting REST Data as “innerHTML” into a Web Application. This is, where I have copied the content of the e2e folder from and changed it a little. Namely:

//e2e/app.e2e-spec.ts
import { AppPage } from './app.po';
import { browser, by, element } from 'protractor';

describe('Blog', () => {

  beforeEach(() => {
    browser.get('/2017/06/24/consuming-a-restful-web-service-with-angular-4/');
  });

  const blog_title = element(by.id('blog_title'));
  const blog_content = element(by.id('blog_content'));

  it('should display the blog title as header 1 and id="blog_title"', () => {
    expect(blog_title.getText()).toEqual('Angular 4 Hello World Quickstart');
  });

  it('should display the blog content', () => {
    expect(blog_content.getInnerHtml()).toMatch(/^<p>In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application./);
  });

});

With this end-to-end test, we expect that the page on /2017/06/13/angular-4-hello-world-with-quickstart/ is displaying the title and content of my WordPress blog Angular 4 Hello World Quickstart.

Step 3.2 Run the end-to-end Tests

Let us try to run e2e tests by issuing following command on the root of the project:

$ protractor

However, we are missing some ingredients. Therefore we will get following response:

**you must either specify a configuration file or at least 3 options. See below for the options:

Usage: protractor [configFile] [options]
configFile defaults to protractor.conf.js
The [options] object will override values from the config file.
See the reference config for a full list of options.

Options:
  --help                                 Print Protractor help menu
  --version                              Print Protractor version
  --browser, --capabilities.browserName  Browsername, e.g. chrome or firefox
  --seleniumAddress                      A running selenium address to use
  --seleniumSessionId                    Attaching an existing session id
  --seleniumServerJar                    Location of the standalone selenium jar file
  --seleniumPort                         Optional port for the selenium standalone server
  --baseUrl                              URL to prepend to all relative paths
  --rootElement                          Element housing ng-app, if not html or body
  --specs                                Comma-separated list of files to test
  --exclude                              Comma-separated list of files to exclude
  --verbose                              Print full spec names
  --stackTrace                           Print stack trace on error
  --params                               Param object to be passed to the tests
  --framework                            Test framework to use: jasmine, mocha, or custom
  --resultJsonOutputFile                 Path to save JSON test result
  --troubleshoot                         Turn on troubleshooting output
  --elementExplorer                      Interactively test Protractor commands
  --debuggerServerPort                   Start a debugger server at specified port instead of repl

Step 3.2.1 Add protractor.conf.js File

We need to add a protractor.conf.js file:

// protractor.conf.js
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:8080/',
  useAllAngular2AppRoots: true,
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

This is the default protractor configuration file that comes with Angular CLI (you will find the file in the base folder of a new Angular CLI project created with ‘ng new-project’). However, we have adapted the parts in blue:

  1. Our application is running on port 8080, so we have changed the default port 4200 to 8080
  2. We have added the useAllAngular2AppRoots: true directive, which should fix an issue I had found on a previous blog Behavior-Driven Angular – part 1: Consuming a RESTful Web Service with Angular 4

Now again:

$ protractor
[20:21:07] E/configParser - Error code: 105
[20:21:07] E/configParser - Error message: failed loading configuration file ./protractor.conf.js
[20:21:07] E/configParser - Error: Cannot find module 'jasmine-spec-reporter'
 at Function.Module._resolveFilename (module.js:469:15)
 at Function.Module._load (module.js:417:25)
 at Module.require (module.js:497:17)
 at require (internal/module.js:20:19)
 at Object.<anonymous> (/protractor/protractor.conf.js:4:26)
 at Module._compile (module.js:570:32)
 at Object.Module._extensions..js (module.js:579:10)
 at Module.load (module.js:487:32)
 at tryModuleLoad (module.js:446:12)
 at Function.Module._load (module.js:438:3)

We need to install jasmine reporter package.

Step 3.2.2 Adapt the package.json File

Instead of a lot of trial&error, which package might be missing, I have decided to copy all missing packages found in in the devDependencies section of a new Angular CLI project into my package.json. This has lead to following additions in blue:

{
  "name": "ng-universal-demo",
  "version": "0.0.0",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/angular/universal-starter.git"
  },
  "contributors": [
    "AngularClass <hello@angularclass.com>",
    "PatrickJS <patrick@angularclass.com>",
    "Jeff Whelpley <jeff@gethuman.com>",
    "Jeff Cross <crossj@google.com>",
    "Mark Pieszak <mpieszak84@gmail.com>",
    "Jason Jean <jasonjean1993@gmail.com>",
    "Fabian Wiles <fabian.wiles@gmail.com>"
  ],
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start:dynamic": "npm run build:dynamic && npm run serve:dynamic",
    "start:static": "npm run build:static && npm run serve:static",
    "build": "ng build",
    "build:client-and-server-bundles": "ng build --prod && ng build --prod --app 1 --output-hashing=false",
    "build:static": "npm run build:client-and-server-bundles && npm run webpack:server && npm run generate:static",
    "build:dynamic": "npm run build:client-and-server-bundles && npm run webpack:server",
    "generate:static": "cd dist && node prerender",
    "webpack:server": "webpack --config webpack.server.config.js --progress --colors",
    "serve:static": "cd dist/browser && http-server",
    "serve:dynamic": "node dist/server"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.2.4",
    "@angular/common": "^4.2.4",
    "@angular/compiler": "^4.2.4",
    "@angular/core": "^4.2.4",
    "@angular/forms": "^4.2.4",
    "@angular/http": "^4.2.4",
    "@angular/platform-browser": "^4.2.4",
    "@angular/platform-browser-dynamic": "^4.2.4",
    "@angular/platform-server": "^4.3.6",
    "@angular/router": "^4.2.4",
    "@nguniversal/express-engine": "^1.0.0-beta.3",
    "@nguniversal/module-map-ngfactory-loader": "^1.0.0-beta.3",
    "core-js": "^2.4.1",
    "rxjs": "^5.4.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "^1.3.0",
    "@angular/compiler-cli": "^4.2.4",
    "@angular/language-service": "^4.2.4",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^8.0.30",
    "codelyzer": "~3.1.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.3.2",
    "cpy-cli": "^1.0.1",
    "http-server": "^0.10.0",
    "reflect-metadata": "^0.1.10",
    "ts-loader": "^2.3.7",
    "typescript": "~2.3.3"
  }
}

Step 3.2.3 Install the new Modules

We need to re-run the installation:

npm i

Step 3.2.4 Re-run the end-to-end Tests

We re-run the protractor test. We still get a bunch of error messages:

$ protractor
[17:39:28] I/direct - Using ChromeDriver directly...
[17:39:28] I/launcher - Running 1 instances of WebDriver
Jasmine started
[17:39:42] E/protractor - Could not find Angular on page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/ : retries looking for angular exceeded

  Blog
    ✗ should display the blog title as header 1 and id="blog_title"
      - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
          at /usr/local/lib/node_modules/protractor/built/browser.js:506:23
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)
      From: Task: Run beforeEach in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:7:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:5:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)
      - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"
          at /usr/local/lib/node_modules/protractor/built/browser.js:272:23
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)Error
          at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/built/element.js:461:27)
          at ElementArrayFinder._this.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:103:30)
          at ElementFinder.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:829:22)
          at Object. (/protractor/e2e/app.e2e-spec.ts:15:34)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25)
      From: Task: Run it("should display the blog title as header 1 and id="blog_title"") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:103:16
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:14:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:5:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)
[17:39:52] E/protractor - Could not find Angular on page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/ : retries looking for angular exceeded
    ✗ should display the blog content
      - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
          at /usr/local/lib/node_modules/protractor/built/browser.js:506:23
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)
      From: Task: Run beforeEach in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:7:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:5:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)
      - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"
          at /usr/local/lib/node_modules/protractor/built/browser.js:272:23
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)Error
          at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/built/element.js:461:27)
          at ElementArrayFinder._this.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:103:30)
          at ElementFinder.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:829:22)
          at Object. (/protractor/e2e/app.e2e-spec.ts:20:25)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25)
      From: Task: Run it("should display the blog content") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:103:16
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:19:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:5:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)

**************************************************
*                    Failures                    *
**************************************************

1) Blog should display the blog title as header 1 and id="blog_title"
  - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
  - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"

2) Blog should display the blog content
  - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
  - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"

Executed 2 of 2 specs (2 FAILED) in 21 secs.
[17:39:52] I/launcher - 0 instance(s) of WebDriver still running
[17:39:52] I/launcher - chrome #01 failed 2 test(s)
[17:39:52] I/launcher - overall: 2 failed spec(s)
[17:39:52] E/launcher - Process exited with error code 1

What is it telling us? Okay, I have forgotten to start the application, before we started the test. Let us correct this now.

Step 3.2.5: Run the Application

Let us run the application as a static universal project as described in this Readme:

$ cd my-project-root
$ npm run start:static
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prestart:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~start:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 start:static /app
> npm run build:static && npm run serve:static

npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prebuild:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~build:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 build:static /app
> npm run build:client-and-server-bundles && npm run webpack:server && npm run generate:static

npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prebuild:client-and-server-bundles: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~build:client-and-server-bundles: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 build:client-and-server-bundles /app
> ng build --prod && ng build --prod --app 1 --output-hashing=false

Date: 2017-10-28T19:27:48.583Z
Hash: c5eeae31051a6225ca92
Time: 15759ms
chunk {0} 0.7ce44253311853d97e73.chunk.js () 1.02 kB {2}  [rendered]
chunk {1} polyfills.80bfeb690703af4fafee.bundle.js (polyfills) 66.1 kB {5} [initial] [rendered]
chunk {2} main.2f46e8d1609d5ba758f8.bundle.js (main) 5.04 kB {4} [initial] [rendered]
chunk {3} styles.d41d8cd98f00b204e980.bundle.css (styles) 0 bytes {5} [initial] [rendered]
chunk {4} vendor.74a477af39cd1230db04.bundle.js (vendor) 305 kB [initial] [rendered]
chunk {5} inline.baceea59185918784cfc.bundle.js (inline) 1.47 kB [entry] [rendered]
Date: 2017-10-28T19:27:58.456Z
Hash: bdee05e0c4e2c172ab79
Time: 5226ms
chunk {0} main.bundle.js (main) 13.6 kB [entry] [rendered]
chunk {1} styles.bundle.css (styles) 0 bytes [entry] [rendered]
npm info lifecycle ng-universal-demo@0.0.0~postbuild:client-and-server-bundles: ng-universal-demo@0.0.0
npm info ok
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prewebpack:server: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~webpack:server: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 webpack:server /app
> webpack --config webpack.server.config.js --progress --colors

 10% building modules 0/2 modules 2 active .../ts-loader/index.js!/app/preHash: 7d5e698066b1d16e7805                                                         Version: webpack 3.7.1
Time: 9365ms
       Asset     Size  Chunks                    Chunk Names
   server.js  4.06 MB       0  [emitted]  [big]  server
prerender.js  3.36 MB       1  [emitted]  [big]  prerender
  [55] ./src lazy 160 bytes {0} {1} [built]
 [104] ./dist/server/main.bundle.js 13.6 kB {0} {1} [built]
 [176] ./server.ts 1.94 kB {0} [built]
 [228] ./src 160 bytes {0} [built]
 [234] (webpack)/buildin/module.js 517 bytes {0} [built]
 [251] ./prerender.ts 2.08 kB {1} [built]
 [253] ./static.paths.js 57 bytes {1} [built]
    + 247 hidden modules
npm info lifecycle ng-universal-demo@0.0.0~postwebpack:server: ng-universal-demo@0.0.0
npm info ok
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~pregenerate:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~generate:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 generate:static /app
> cd dist && node prerender

npm info lifecycle ng-universal-demo@0.0.0~postgenerate:static: ng-universal-demo@0.0.0
npm info ok
npm info lifecycle ng-universal-demo@0.0.0~postbuild:static: ng-universal-demo@0.0.0
npm info ok
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~preserve:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~serve:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 serve:static /app
> cd dist/browser && http-server

Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://172.31.21.180:8080
  http://172.17.0.1:8080

Step 3.2.6: Repeat the end-to-end Tests

If we then run protractor in the other terminal, we will see that the error messages have not changed:

$ protractor
...
1) Blog should display the blog title as header 1 and id="blog_title"
  - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
  - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"

2) Blog should display the blog content
  - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
  - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"
...

The error messages have not changed compared to the situation before. However, in the other terminal, we can see that the NodeJS server understands the requests and answers with a “404 Not found”, since we have not yet implemented the feature:

...
Available on:
  http://127.0.0.1:8080
  http://172.31.21.180:8080
  http://172.17.0.1:8080
[Sat Oct 28 2017 19:28:45 GMT+0000 (UTC)] "GET /blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"
[Sat Oct 28 2017 19:28:45 GMT+0000 (UTC)] "GET /blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/" Error (404): "Not found"
[Sat Oct 28 2017 19:28:45 GMT+0000 (UTC)] "GET /favicon.ico" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"
[Sat Oct 28 2017 19:28:55 GMT+0000 (UTC)] "GET /blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"
[Sat Oct 28 2017 19:28:55 GMT+0000 (UTC)] "GET /blog/2017/06/24/consuming-a-restful-web-service-with-angular-4/" Error (404): "Not found"

Let us assume that the protractor errors will vanish, once we have implemented the service correctly.

Step 4: Implement the Feature

Step 4.1 Create Link

In src/app/app.component.ts, we add a link to the single blog post as defined in the spec:

On port 8080, we can see that the new link is visible:

However, if we klick the link, nothing happens. When pressing F12 and repeating the click, we get the error message:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'blog/2017/06/13/angular-4-hello-world-with-quickstart'
Error: Cannot match any routes. URL Segment: 'blog/2017/06/13/angular-4-hello-world-with-quickstart'

A route to the link is missing.

Step 4.2 Add Route

Let us add a route now.

If we restart the server, we get the message:

ERROR in Error: Could not resolve "./blog/blog.module" from "/app/src/app/app.module.ts".

This is expected since the file does not exist yet. Let us change that now:

$ cp -R src/app/lazy src/app/blog
$ mv src/app/blog/lazy.module.ts src/app/blog/blog.module.ts
$ sed -r -i "s/lazy/blog/g" src/app/blog/blog.module.ts
$ sed -r -i "s/i'm blog/i'm a blog/g" src/app/blog/blog.module.ts
$ sed -r -i "s/Lazy/Blog/g" src/app/blog/blog.module.ts

This will copy the lazy component to a blog component.

Now the error message is gone and we get the following output in a browser, if we click the link:

However, we the protractor messages do not change at all. We still get the error messages:

$ protractor
...
1) Blog should display the blog title as header 1 and id="blog_title"
  - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/13/angular-4-hello-world-with-quickstart. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
  - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"

2) Blog should display the blog content
  - Failed: Angular could not be found on the page http://localhost:8080/blog/2017/06/13/angular-4-hello-world-with-quickstart. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
  - Failed: Error while waiting for Protractor to sync with the page: "window.getAllAngularTestabilities is not a function"

Why is this the case? I would have expected the error message to tell us that the page is available with the content not being the one expected by the spec. However, if we run the end-to-end tests, we can observe that the link is still returning a “404 Not found” message:

[Sat Oct 28 2017 22:02:19 GMT+0000 (UTC)] "GET /blog/2017/06/13/angular-4-hello-world-with-quickstart" Error (404): "Not found"

This is the case, although the link seems to be reachable within the browser, if we click the link named “Angular 4 Hello World Quickstart:

In debug mode (F12) we see:

However, if we press the reload the page in the browser, we get an empty page:

After finishing the debug mode and reloading the page, we get a “404 Not found” message:

That is interesting: The link /blog/2017/06/13/angular-4-hello-world-with-quickstart is reached by client-side routing by clicking on the link, but a reload of the page fails. Let us fix that now.

Step 4.2.1 Fix the Error: 404 Not found

The error is caused by the fact that we are running the service in static mode, but we have not taken any measures that the path is created in the HTTP server yet.

Fixing the error is as simple as

  • adding the link to static.paths.js link
  • re-starting the server with npm run start:static

The static.paths.js file is located in the project’s root:

// static.paths.js
module.exports = [
  '/',
  '/lazy',
  '/lazy/nested',
  '/blog/2017/06/13/angular-4-hello-world-with-quickstart'
];

Now we create the missing path via:

npm run start:static

This is running the command npm run build:static, which is running the command cd dist && node prerender (among others). This will create the additional path in the directory tree (in blue):

$ yum install -y tree # if not installed already
...
$ tree dist/
dist/
├── browser
│   ├── 0.7ce44253311853d97e73.chunk.js
│   ├── 1.1fd3684dd14207827a93.chunk.js
│   ├── 3rdpartylicenses.txt
│   ├── blog
│   │   └── 2017
│   │       └── 06
│   │           └── 13
│   │               └── angular-4-hello-world-with-quickstart
│   │                   └── index.html
│   ├── favicon.ico
│   ├── index.html
│   ├── inline.a70b6ebe7ee886967e09.bundle.js
│   ├── lazy
│   │   ├── index.html
│   │   └── nested
│   │       └── index.html
│   ├── main.2d468591087d33c2e372.bundle.js
│   ├── polyfills.54dd1bb0dea7bab42697.bundle.js
│   ├── styles.d41d8cd98f00b204e980.bundle.css
│   └── vendor.b2c3f787d02157b98c0e.bundle.js
├── prerender.js
├── server
│   ├── favicon.ico
│   ├── main.bundle.js
│   └── styles.bundle.css
└── server.js

9 directories, 18 files

From now on, we can directly access the path /blog/2017/06/13/angular-4-hello-world-with-quickstart on the server. If we access the path, we see the following line in the server log:

[Sat Oct 28 2017 22:51:25 GMT+0000 (UTC)] "GET /blog/2017/06/13/angular-4-hello-world-with-quickstart" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"

In the browser, we can directly access the path and we see that the server is serving the path. We not only can click the “Angular 4 Hello World Quickstart link” from the root path, but we also can reload the page. We can see in the browser’s debug window (press F12) that the corresponding path is known to the server:

Finally, as expected, the protractor output changes:

$ protractor
...
1) Blog should display the blog title as header 1 and id="blog_title"
  - Failed: No element found using locator: By(css selector, *[id="blog_title"])

2) Blog should display the blog content
  - Failed: No element found using locator: By(css selector, *[id="blog_content"])

This is expected: we have created a static page with some dummy content, but we have not yet downloaded and displayed the content of the WordPress blog post.

Now let us save the changes on GIT:

git add .
git commit -m'added blog module and added links to blog module on app and static.paths.js'

Step 4.3: Create HTML Template for the Blog

The specs are expecting an HTML template with a blog title in an element with the ID blog_title and a blog content in an element with the ID blog_content. Let us create that now. We create a new file:

To connect the new HTML template with the rest, we need to add a templateURL reference the blog.module.ts file:

// src/app/blog/blog.module.ts
import {NgModule, Component} from '@angular/core'
import {RouterModule} from '@angular/router'

@Component({
  selector: 'blog-view',
  templateUrl: './blog.component.html'
})
export class BlogComponent {}

@NgModule({
  declarations: [BlogComponent],
  imports: [
    RouterModule.forChild([
      { path: '', component: BlogComponent, pathMatch: 'full'}
    ])
  ]
})
export class BlogModule {

}

After a restart of the server, the protractor output changes:

$ protractor
...
1) Blog should display the blog title as header 1 and id="blog_title"
  - Expected 'This is the blog title' to equal 'Angular 4 Hello World Quickstart'.

2) Blog should display the blog content
  - Expected 'This is the blog content' to contain 'In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application.'.
  - Expected 'This is the blog content' to match /^<p>In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application.'.

Now let us save the result:

git add .
git commit -m'added blog HTML template'

Step 4.4: Create Variables

We change the HTML template, so it used variables we will create thereafter:

The variables are not yet defined. Therefore we would get following error message, if we reload the server:

ERROR in /app/src/$$_gendir/app/blog/blog.module.ngfactory.ts (34,31): Property 'title' does not exist on type 'BlogComponent'.
ERROR in ng:///app/src/app/blog/blog.component.html (3,1): Property 'content' does not exist on type 'BlogComponent'.

Let us define the variables:

Step 4.5: Read Variables from the WordPress API

Now we read in the HTTP content into the variables using Observables.

// src/app/blog/blog.module.ts
import {NgModule, Component, OnInit} from '@angular/core'
import {RouterModule} from '@angular/router'
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map'
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'blog-view',
  templateUrl: './blog.component.html'
})

export class BlogComponent implements OnInit {

  title : String = "Loading..."
  content : String = "Loading..."

  constructor(private _http: Http) {}

  ngOnInit() {
     this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/oliverveits.wordpress.com/posts/3078')
                .map((res: Response) => res.json())
                 .subscribe(data => {
                        this.title = data.title;
                        this.content = data.content;
                        console.log(data);
                });
  }

}

import { HttpModule } from '@angular/http';
@NgModule({
  declarations: [BlogComponent],
  imports: [
    HttpModule,
    RouterModule.forChild([
      { path: '', component: BlogComponent, pathMatch: 'full'}
    ])
  ]
})
export class BlogModule {

}

There, we have defined a private variable for the HTTP service. This service is used to create an observable with the GET function, which we subscribe to read the title and content of a single post into the variable. Moreover, we have defined the provider for HttpModule in the BlogModule part. See this blog post for a quick introduction to this concept. More in-depth step-by-step instructions including end-to-end tests can be found in part 1 and part 2 of the Behavior-driven Angular series.

After restarting the server we already can see the title and content of the blog post retrieved via WordPress API:

npm run start:static

Let us test the result with ‘protractor’:

$ protractor
[17:44:30] I/direct - Using ChromeDriver directly...
[17:44:30] I/launcher - Running 1 instances of WebDriver
Jasmine started

  Blog
    ✓ should display the blog title as header 1 and id="blog_title"
    ✓ should display the blog content

Executed 2 of 2 specs SUCCESS in 1 sec.
[17:44:35] I/launcher - 0 instance(s) of WebDriver still running
[17:44:35] I/launcher - chrome #01 passed

This was successful, this time!

Step 5: Verify Server-Side Rendering

The reason why we have chosen Angular Universal is its server-side rendering feature. In situations with low bandwidth to the Internet, server-side rendering helps us to provide the user with the content of the page with a much lower latency.

To be sure that server-side rendering works as expected, we review the HTML source manually:

Unfortunately, server-side rendering does not seem to work the way expected. We still see the “Loading…” directive instead of the innerHTML content. Re-starting the server will reveal some errors in the log:

$ npm run start:static
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prestart:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~start:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 start:static /app
> npm run build:static && npm run serve:static

npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prebuild:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~build:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 build:static /app
> npm run build:client-and-server-bundles && npm run webpack:server && npm run generate:static

npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prebuild:client-and-server-bundles: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~build:client-and-server-bundles: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 build:client-and-server-bundles /app
> ng build --prod && ng build --prod --app 1 --output-hashing=false

Date: 2017-10-29T17:50:51.072Z
Hash: 36c4b63a2e9bf0ceb4c9
Time: 14508ms
chunk {0} 0.7ce44253311853d97e73.chunk.js () 1.02 kB {1} {3}  [rendered]
chunk {1} 1.e11ff4adfdb6d0ed7929.chunk.js () 21.1 kB {0} {3}  [rendered]
chunk {2} polyfills.54dd1bb0dea7bab42697.bundle.js (polyfills) 66.1 kB {6} [initial] [rendered]
chunk {3} main.2d468591087d33c2e372.bundle.js (main) 5.76 kB {5} [initial] [rendered]
chunk {4} styles.d41d8cd98f00b204e980.bundle.css (styles) 0 bytes {6} [initial] [rendered]
chunk {5} vendor.9a16430f75eb51537ae8.bundle.js (vendor) 305 kB [initial] [rendered]
chunk {6} inline.1735ca01d11efd0014a9.bundle.js (inline) 1.5 kB [entry] [rendered]
Date: 2017-10-29T17:50:59.897Z
Hash: cd969720fe342e3bf65d
Time: 5221ms
chunk {0} main.bundle.js (main) 17.1 kB [entry] [rendered]
chunk {1} styles.bundle.css (styles) 0 bytes [entry] [rendered]
npm info lifecycle ng-universal-demo@0.0.0~postbuild:client-and-server-bundles: ng-universal-demo@0.0.0
npm info ok
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~prewebpack:server: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~webpack:server: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 webpack:server /app
> webpack --config webpack.server.config.js --progress --colors

 10% building modules 0/2 modules 2 active .../ts-loader/index.js!/app/preHash: 46749ef5d0e4cdd33844                                                         Version: webpack 3.7.1
Time: 9306ms
       Asset     Size  Chunks                    Chunk Names
   server.js  4.07 MB       0  [emitted]  [big]  server
prerender.js  3.36 MB       1  [emitted]  [big]  prerender
  [56] ./src lazy 160 bytes {0} {1} [built]
 [104] ./dist/server/main.bundle.js 17.1 kB {0} {1} [built]
 [177] ./server.ts 1.94 kB {0} [built]
 [229] ./src 160 bytes {0} [built]
 [235] (webpack)/buildin/module.js 517 bytes {0} [built]
 [252] ./prerender.ts 2.08 kB {1} [built]
 [254] ./static.paths.js 117 bytes {1} [built]
    + 248 hidden modules
npm info lifecycle ng-universal-demo@0.0.0~postwebpack:server: ng-universal-demo@0.0.0
npm info ok
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~pregenerate:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~generate:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 generate:static /app
> cd dist && node prerender

ERROR Error: not implemented
    at Parse5DomAdapter.getCookie (/app/dist/prerender.js:37285:68)
    at CookieXSRFStrategy.configureRequest (/app/dist/prerender.js:41698:119)
    at XHRBackend.createConnection (/app/dist/prerender.js:41747:28)
    at httpRequest (/app/dist/prerender.js:42155:20)
    at Http.request (/app/dist/prerender.js:42265:34)
    at Http.get (/app/dist/prerender.js:42279:21)
    at e.X+Mx.e.ngOnInit (/app/dist/prerender.js:78900:8159)
    at checkAndUpdateDirectiveInline (/app/dist/prerender.js:11698:19)
    at checkAndUpdateNodeInline (/app/dist/prerender.js:13196:20)
    at checkAndUpdateNode (/app/dist/prerender.js:13139:16)
npm info lifecycle ng-universal-demo@0.0.0~postgenerate:static: ng-universal-demo@0.0.0
npm info ok
npm info lifecycle ng-universal-demo@0.0.0~postbuild:static: ng-universal-demo@0.0.0
npm info ok
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info lifecycle ng-universal-demo@0.0.0~preserve:static: ng-universal-demo@0.0.0
npm info lifecycle ng-universal-demo@0.0.0~serve:static: ng-universal-demo@0.0.0

> ng-universal-demo@0.0.0 serve:static /app
> cd dist/browser && http-server

Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://172.31.21.180:8080
  http://172.17.0.1:8080
Hit CTRL-C to stop the server

The dynamic server __npm run start:dynamic__ has the same problems. The only difference is that the error message appears every time the link is clicked.

After a lot of googling (in vain) and testing, I finally came up with a workaround: if we move out the HttpModule import from the BlogModule to the AppModule, the client-side-rendering as well as the server-side-rendering work fine:

// src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';

import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
  ],
  imports: [
    HttpModule,
    BrowserModule.withServerTransition({appId: 'my-app'}),
    RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full'},
      { path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule'},
      { path: 'lazy/nested', loadChildren: './lazy/lazy.module#LazyModule'},
      { path: 'blog/2017/06/13/angular-4-hello-world-with-quickstart', loadChildren: './blog/blog.module#BlogModule'}
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

The corresponding lines need to be removed in the BlogModule. Otherwise, the error messaged do not disappear (I would have preferred to them for portability of the blog component, since I do not want the blog component to depend on imports of an upstream component; however, I am forced to remove it anyway, it seems):

// src/app/blog/blog.module.ts
import {NgModule, Component, OnInit} from '@angular/core'
import {RouterModule} from '@angular/router'
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map'
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'blog-view',
  templateUrl: './blog.component.html'
})

export class BlogComponent implements OnInit {

  title : String = "Loading..."
  content : String = "Loading..."

  constructor(private _http: Http) {}

  ngOnInit() {
     this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/oliverveits.wordpress.com/posts/3078')
                .map((res: Response) => res.json())
                 .subscribe(data => {
                        this.title = data.title;
                        this.content = data.content;
                        console.log(data);
                });
  }

}

//import { HttpModule }    from '@angular/http'; // moved to src/app/app.module.ts

@NgModule({
  declarations: [BlogComponent],
  imports: [
    //HttpModule, // moved to src/app/app.module.ts
    RouterModule.forChild([
      { path: '', component: BlogComponent, pathMatch: 'full'}
    ])
  ]
})
export class BlogModule {
}

Now, after restarting the server, the blog post page is visible in both ways:

client-side rendering:

For testing client-side rendering, we open the root URL localhost:8080/

server-side rendering:

For testing the server-side rendering, we need to access the URL directly, e.g. by reloading the page we see above (e.g. press F5) or by cutting&pasting the full URL into the browser.

We see the following:

–> the page displays the full content after less than a second

–> the page switches over to client-side rendering and the “Loading…” appears

–> once the page has been retrieved from the WordPress API, the full content is visible again

As the last test before re-running the end-to-end tests, we can see that the blog title and content can be seen in the HTML source:

This is both, SEO-friendly and the content will show up much quicker on mobile devices with a low-bandwidth Internet connection compared to the client-side rendering case.

Note that the protractor tests are still successful:

$ protractor
[17:44:30] I/direct - Using ChromeDriver directly...
[17:44:30] I/launcher - Running 1 instances of WebDriver
Jasmine started

  Blog
    ✓ should display the blog title as header 1 and id="blog_title"
    ✓ should display the blog content

Executed 2 of 2 specs SUCCESS in 1 sec.
[17:44:35] I/launcher - 0 instance(s) of WebDriver still running
[17:44:35] I/launcher - chrome #01 passed

This was successful again.

Summary

We have successfully created an Angular Universal application based on Angular CLI with a REST client feeding in a single blog post from the WordPress API. We had experienced some trouble with server-side rendering, which was resolved miraculously by moving the HttpModule import from the BlogModule (the module using HTTP) to the root AppModule. At the end, we have succeeded to create an application that loads the page from the server and hands over the control to the browser thereafter.

ToDo:

  • dynamic path: I would like that all paths /blog/xxx are available and contain the title and content of the corresponding WordPress link https://oliverveits.wordpress.com/xxx/
  • Refactoring:
    • Move the REST client into a separate ‘@Injectable’ service.
    • separate the blog module from the blog component (small effort, but low priority)
  • In order to test server-side rendering, I had to restart the server every time the code has changed. I need to find a better way to handle this in future: e.g. use continuous testing for development with client-side rendering, and let the continuous integration machinery (e.g. TravisCI or CircleCI) perform the full tests in productive mode after each GIT push.
  • In future, I have to find out, how to write tests that will fail if the server-side rendering does not work. This time, I had to manually review, whether the HTML source contains the content.
0

Behavior-Driven Angular – Part 2: Inserting REST Data as “innerHTML” into a Web Application

Today, we will extend the behavior-driven development example of the previous blog post and add the blog content to the document. Like last time, we will retrieve the HTML content from the WordPress API. Sounds easy, right? We will see that the challenge is to display the HTML content correctly, so we do not see escaped HTML like “<p>…” on the page.

As in part 1, we will follow a “test first” strategy: we will create the e2e test specification before we implement the actual code.

Within the Protractor/Jasmine framework, we will learn how to match the text and the inner HTML of browser DOM elements with functions like expect(...).toEqual("..."), .toContain("...") and .toMatch(/regex/) functions. The latter gives us the full flexibility of regular expressions.

Check out this book on Amazon: Angular Test-Driven Development

Plan for Today

Today, we plan to complement the blog title we have shown last time with the blog content, similar to the blog post Angular 4 Hello World Quickstart, which we will uses as our data mine. We will only show the title and the content as follows:

Before we start coding, we will add an e2e test that defines our expectation.

Step 0: Clone the GIT Repository and install the Application

This step can be skipped if you have followed part 1 of this series.

I am assuming that you have a Docker host available with 1.5GB or more RAM, GIT is installed on that host.

alias cli='docker run -it --rm -w /app -v $(pwd):/app -p 4200:4200 oveits/angular-cli:1.4.3 $@'
alias protractor='docker run -it --privileged --rm --net=host -v /dev/shm:/dev/shm -v $(pwd):/protractor webnicer/protractor-headless $@'
git clone https://github.com/oveits/consuming-a-restful-web-service-with-angular.git
cd consuming-a-restful-web-service-with-angular
git checkout -b 320ae88
cli npm i
chown -R $(whoami) .
cli ng serve --host 0.0.0.0

Phase 1: Create an e2e Test

Step 1.1: Create a GIT Feature Branch

As always with a new feature, let us create a feature branch (on a second terminal):

$ cd /vagrant/consuming-a-restful-web-service-with-angular/

$ protractor
[20:24:22] I/direct - Using ChromeDriver directly...
[20:24:22] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display the title

Executed 1 of 1 spec SUCCESS in 0.756 sec.
[20:24:27] I/launcher - 0 instance(s) of WebDriver still running
[20:24:27] I/launcher - chrome #01 passed

$ git checkout -b feature/0004-add-blog-content

You might need to adapt the path to your project. The protractor command is optional, but it will ensure that the e2e tests have worked on your machine before you start changing the code. I have seen some permissions topic described in the Appendices, which have made me cautious.

Step 1.2 (optional): Apply new Test Functions to the Blog Title

We would like to add a test that checks, whether the blog content is showing on the page. There are many Jasmine specification examples out there. Somehow, I have stumbled over this example. In order to verify that the functions I found there work fine, I thought it would be a good idea to write a new test similar to the ones in the example, but apply the test to the blog title before we write a new test for the blog content. This way, we can verify that we apply the correct syntax.

I have kept the original specification code, but I have added following code to the spec:

// e2e/app.e2e-spec.ts
import { browser, by, element } from 'protractor';
import { AppPage } from './app.po';

describe('consuming-a-restful-web-service-with-angular App', () => {
  let page: AppPage;

  beforeEach(() => {
    page = new AppPage();
  });

  it('should display the title', () => {
    page.navigateTo();
    expect(page.getParagraphText()).toContain('Angular 4 Hello World Quickstart');
  });
});

describe('Blog', () => {

  beforeEach(() => {
    browser.get('/');
  });

  it('should display the blog title as header 1 and id="blog_title"', () => {
    expect(element(by.css('h1')).getText()).toEqual('Angular 4 Hello World Quickstart');
  });
});

Both protractor e2e tests are successful without changing the code:

$ protractor
[20:59:51] I/direct - Using ChromeDriver directly...
[20:59:51] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display the title

  Blog
    ✓ should display the blog title as header 1 and id="blog_title"

Executed 2 of 2 specs SUCCESS in 2 secs.
[20:59:57] I/launcher - 0 instance(s) of WebDriver still running
[20:59:57] I/launcher - chrome #01 passed

Save it. Note: for pushing the changes to Github, you will need to fork my project and work with your fork. Otherwise, you can keep the git backups locally only.

git commit -am'1.2 added an addional test for the title looking for the first H1 header (successful test)'
git push

Step 1.3 (optional): Refine the Test

Step 1.3.1 Create a Test looking for a specific Element per ID

Since the blog content will not be a header, we will need to look for something, which is unique on the page. We use an ID for fetching the correct element from the page:

import { browser, by, element } from 'protractor';

...

describe('Blog', () => {

  beforeEach(() => {
    browser.get('/');
  });

  const blog_title = element(by.id('blog_title'));

  it('should display the blog title as header 1 and id="blog_title"', () => {
    expect(element(by.css('h1')).getText()).toEqual('Angular 4 Hello World Quickstart');
    expect(blog_title.getText()).toEqual('Angular 4 Hello World Quickstart');
  });
});

Now the protractor test will fail. This is because we have not set the ID on the HTML template yet:

$ protractor
[21:07:51] I/direct - Using ChromeDriver directly...
[21:07:51] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display the title

  Blog
    ✗ should display the blog title as header 1 and id="blog_title"
      - Failed: No element found using locator: By(css selector, *[id="blog_title"])
          at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
          at NoSuchElementError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:242:5)
          at /usr/local/lib/node_modules/protractor/built/element.js:808:27
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)Error
          at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/built/element.js:461:27)
          at ElementArrayFinder._this.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:103:30)
          at ElementFinder.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:829:22)
          at Object. (/protractor/e2e/app.e2e-spec.ts:28:23)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25)
      From: Task: Run it("should display the blog title as header 1 and id="blog_title"") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:16:5
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:26:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:18:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)

**************************************************
*                    Failures                    *
**************************************************

1) Blog should display the blog title as header 1 and id="blog_title"
  - Failed: No element found using locator: By(css selector, *[id="blog_title"])

Executed 2 of 2 specs (1 FAILED) in 2 secs.
[21:07:58] I/launcher - 0 instance(s) of WebDriver still running
[21:07:58] I/launcher - chrome #01 failed 1 test(s)
[21:07:58] I/launcher - overall: 1 failed spec(s)
[21:07:58] E/launcher - Process exited with error code 1

To save the change:

git commit -am'1.3.1 search title by element id (failed e2e test)'

Step 1.3.2 Fix the Test

Let us fix the failed test like follows: In the HTML template src/app/app.component.html, we specify the element ID:

<h1 id="blog_title">{{title}}</h1>

Now the protractor test is successful again:

$ protractor
[21:14:27] I/direct - Using ChromeDriver directly...
[21:14:27] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display the title

  Blog
    ✓ should display the blog title as header 1 and id="blog_title"

Executed 2 of 2 specs SUCCESS in 2 secs.
[21:14:34] I/launcher - 0 instance(s) of WebDriver still running
[21:14:34] I/launcher - chrome #01 passed

That was simple. Now let us apply our learnings to the blog content.

To save the change:

git commit -am'1.3.2 add ID to HTML template (success)'; git push

Phase 2: Create the Test for the Blog Content

The content of the blog can be seen on WordPress:

The content starts with: In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application.

Let us search for that on our application.

Step 2.1 Add Blog Content e2e Tests

Similar to what we have done for the Blog Title, let us create an e2e test for the blog content. We add the parts in blue to e2e/app.e2e-spec.ts:

// e2e/app.e2e-spec.ts
...
describe('Blog', () => {

  beforeEach(() => {
    browser.get('/');
  });

  const blog_title = element(by.id('blog_title'));
  const blog_content = element(by.id('blog_content'));

  it('should display the blog title as header 1 and id="blog_title"', () => {
    expect(element(by.css('h1')).getText()).toEqual('Angular 4 Hello World Quickstart');
    expect(blog_title.getText()).toEqual('Angular 4 Hello World Quickstart');
  });

  it('should display the blog content', () => {
    expect(blog_content.getText()).toContain('In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application.');
  });
});

Since the content is quite large, we did not compare it with the equality operator, but we have used the ‘toContain’ function instead.

The new protractor test fails as expected:

$ protractor
[21:23:04] I/direct - Using ChromeDriver directly...
[21:23:04] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display the title

  Blog
    ✓ should display the blog title as header 1 and id="blog_title"
    ✗ should display the blog content
      - Failed: No element found using locator: By(css selector, *[id="blog_content"])
          at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
          at NoSuchElementError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:242:5)
          at /usr/local/lib/node_modules/protractor/built/element.js:808:27
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)Error
          at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/built/element.js:461:27)
          at ElementArrayFinder._this.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:103:30)
          at ElementFinder.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:829:22)
          at Object. (/protractor/e2e/app.e2e-spec.ts:33:25)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25)
      From: Task: Run it("should display the blog content") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:16:5
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:32:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:18:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)

**************************************************
*                    Failures                    *
**************************************************

1) Blog should display the blog content
  - Failed: No element found using locator: By(css selector, *[id="blog_content"])

Executed 3 of 3 specs (1 FAILED) in 3 secs.
[21:23:12] I/launcher - 0 instance(s) of WebDriver still running
[21:23:12] I/launcher - chrome #01 failed 1 test(s)
[21:23:12] I/launcher - overall: 1 failed spec(s)
[21:23:12] E/launcher - Process exited with error code 1

To save the change:

git commit -am'2.1 add test for blog content (failed)'; git push

Step 2.2 Fix the Blog Content Test

Let us fix the test now.

Step 2.2.1 Add the Blog Content to the HTML Template

In order to display the blog content, we need to add the following to the HTML template src/app/app.component.html:

Step 2.2.2 Define the Variable ‘content’ in the Component

However, as long as the variable ‘content’ is not defined, we will have added an empty div. To define the variable, we must change the component src/app/app.component.ts

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { Response } from '@angular/http';
import 'rxjs/add/operator/map'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  title : any = null
  content : any = null

  constructor(private _http: Http) {}

  ngOnInit() {
     this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/oliverveits.wordpress.com/posts/3078')
                .map((res: Response) => res.json())
                 .subscribe(data => {
                        this.title = data.title;
                        this.content = data.content;
                        console.log(data);
                });
  }
}

That’s it: the e2e tests are successful:

$ protractor
[21:30:12] I/direct - Using ChromeDriver directly...
[21:30:12] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display the title

  Blog
    ✓ should display the blog title as header 1 and id="blog_title"
    ✓ should display the blog content

Executed 3 of 3 specs SUCCESS in 3 secs.
[21:30:19] I/launcher - 0 instance(s) of WebDriver still running
[21:30:19] I/launcher - chrome #01 passed

To save the change:

git commit -am'2.2.2 Added content to HTML template and component (success)'; git push

Step 2.3 Explore the Result

Now let us have a look at what we have accomplished and let us open the browser on http://localhost:4200:

The good news is: the content is there.

😉

The bad news it: it is not readable because the HTML code in the blog content variable has been HTML escaped.

🙁

This is the standard behavior in Angular. So what can we do now? The solution to the problem can be found in Step 2.3 of my original post: we need to set the innerHTML of the div instead of adding the content as text. But, as we are performing a “behavior-driven” approach, let us try to write the tests first.

Step 2.4 Improve the e2e Test Spec

Let us add an additional line to the test specification in order to make sure, we will see the HTML in the correct format:

import { browser, by, element } from 'protractor';

describe('Blog', () => {

  beforeEach(() => {
    browser.get('/');
  });

  const blog_title = element(by.id('blog_title'));
  const blog_content = element(by.id('blog_content'));

  it('should display the blog title as header 1 and id="blog_title"', () => {
    expect(element(by.css('h1')).getText()).toEqual('Angular 4 Hello World Quickstart');
    expect(blog_title.getText()).toEqual('Angular 4 Hello World Quickstart');
  });

  it('should display the blog content', () => {
    expect(blog_content.getText()).toContain('In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application.');
    
  });
});

With that, we test, whether the innerHTML of the div element starts with the correct HTML code. For that, we have made use of two functionalities of Jasmine:

  1. reading the innerHTML of an element with the getInnerHtml() function
  2. matching against a regular expression with toMatch(/regexp/)

As expected, the protractor test fails with the message

To save the change:

git commit -am'2.4 added innerHTML test for content with regular expression (fail)'; git push

Step 2.5 Fulfill the improved e2e Test

We can see that the content is escaped (e.g.  instead of ). Let us fix that by specifying the innerHTML like follows:

As soon as the content is loaded, the innerHTML ‘Loading…’ will be replaced by the content retrieved from WordPress.

Let us run the test:

$ protractor
[20:55:50] I/direct - Using ChromeDriver directly...
[20:55:50] I/launcher - Running 1 instances of WebDriver
Jasmine started
[20:55:56] W/element - more than one element found for locator By(css selector, app-root h1) - the first result will be used

  consuming-a-restful-web-service-with-angular App
    ✓ should display blog title

[20:55:57] W/element - more than one element found for locator By(css selector, h1) - the first result will be used
  Blog
    ✓ should display the blog title as header 1 and id="blog_title"
    ✓ should display the blog content

Executed 3 of 3 specs SUCCESS in 3 secs.
[20:55:57] I/launcher - 0 instance(s) of WebDriver still running
[20:55:57] I/launcher - chrome #01 passed

That was easy, again.

To save the change:

git commit -am'2.5 Fix the content innerHTML test (success)'; git push

Step 3: Explore the Final Result

Now let us head over to the browser on URL http://localhost:4200 again:

Even though there is no styling implemented yet, that looks much better now. This is, what we had in mind to implement today.

 

As a wrap-up, the changes can be merged into the develop branch: the tests are successful and also the explorative “tests” have shown a correct result.


git checkout develop
git merge feature/0004-add-blog-content
git push

Summary

In this blog post, we have shown how to retrieve HTML-formated data from the WordPress API and display it in a correct format. In a “test-driven” approach, we have created Protractor e2e test specifications, before we have implemented the function.

Appendix: Error message: failed loading configuration file ./protractor.conf.js

After successfully cloning and installing the repo, I had seen following error message, when trying to perform the e2e tests:

$ protractor
[19:23:16] E/configParser - Error code: 105
[19:23:16] E/configParser - Error message: failed loading configuration file ./protractor.conf.js
[19:23:16] E/configParser - Error: Cannot find module 'jasmine-spec-reporter'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object. (/protractor/protractor.conf.js:4:26)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Resolution:

I have seen that the cli command was creating all files as user root. This was because I had defined

alias cli='docker run -it --rm -w /app -v $(pwd):/app oveits/angular-cli:1.4.3 $@'

After changing this to

alias cli='docker run -it --rm -w /app -v $(pwd):/app -p 4200:4200 -u $(id -u $(whoami)) oveits/angular-cli:1.4.3 $@'

and re-performing the cli npm i after the clone, the problem was resolved. However, this has caused the next ‘npm i’ issue described below, and it is better to perform following workaround:

Better:

  1. Keep the first version of the alias
  2. After applying ‘cli npm i’, perform the command sudo chown -R $(whoami) PROJECT_ROOT_DIR .

Appendix npm i: Error: EACCES: permission denied, mkdir ‘/.npm’

npm ERR! Linux 4.2.0-42-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "i"
npm ERR! node v6.11.2
npm ERR! npm  v3.10.10
npm ERR! path /.npm
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir

npm ERR! Error: EACCES: permission denied, mkdir '/.npm'
npm ERR!     at Error (native)
npm ERR!  { Error: EACCES: permission denied, mkdir '/.npm'
npm ERR!     at Error (native)
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/.npm',
npm ERR!   parent: 'consuming-a-restful-web-service-with-angular' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /app/npm-debug.log

The reason is, that I had defined

alias cli='docker run -it --rm -w /app -v $(pwd):/app -p 4200:4200 -u $(id -u $(whoami)) oveits/angular-cli:1.4.3 $@'

With that, npm i is run as the vagrant user with ID=900. However, inside the container, neither the user “vagrant” nor the user ID 900 is defined. This seems to cause the problem that the cli npm i command wants to create a directory /$HOME/.npm, but $HOME is not set. Therefore, the user with id=900 wants to create the file /.npm, but only root is allowed to do so.

The better workaround is to define

alias cli='docker run -it --rm -w /app -v $(pwd):/app -p 4200:4200 oveits/angular-cli:1.4.3 $@'

without the -u option and perform a command

chown -R $(whoami) .

where needed (e.g. after each npm i command).

1

Behavior-Driven Angular – part 1: Consuming a RESTful Web Service with Angular 4

In this step-by-step tutorial, we will follow a behavior-driven development approach to create an Angular 4 application from Angular CLI. The hello-world-like application will consume the WordPress REST API and it will display a blog post title. We will create and run end-to-end test scripts that simulate the customer behavior on a Chrome browser within a Protractor headless Docker container.

As a side feature of this tutorial, we will demonstrate basic Git handling: we will learn how to create a GIT Repository, create a feature branch, commit code changes, and merge the tested and fully functional feature branch into the main development branch.

Check out this book on Amazon: Angular Test-Driven Development

Introduction

My post Consuming a RESTful Web Service with Angular 4 has grown much more popular than expected. Thanks a lot for your views! The September ist still not finished, and the article has achieved more than 3000 views in its fourth month. I hope the trend will keep on:

😉

So, why would I want to rework a blog post that seemingly plays a chord in the developer’s community? The reasons I have started to rework the example are:

  • I have come to a point, where I had the need to refactor the code. However, I do not like refactoring before I do not have a good coverage of end-to-end tests. This was fixed easily in my previous blog post Angular end-to-end Testing.
  • The next topic was not so easy to be resolved: I had created a working example, but when I have created a GIT repository from it, Angular CLI had a problem with new clones of that code. An Angular problem I could not resolve easily and it looked like I had to start from scratch. This is, what I am doing now, committing many snapshots to GIT. If I so, why not explaining to my audience, what I am doing and why? This way, the current post has become an example that demonstrates basic GIT handling.

This blog post will fix those two issues, I deem.

Even if I am tempted to automate many of the development process steps, we will keep it simple without the usage of DevOps tools like Jenkins with BitBucket, Sonar, BrowserStack, JMeter Integration and Docker data center integration you would find in real-world agile projects. Some of such topics can be explored in more detail on my other blog posts on Jenkins (explore the “Jenkins Tutorial” drop-down menu of my blog).

Why behavior driven development?

I have made a very good experience with behavior driven development (BDD), or “test first” development. Some years ago, I have applied this principle on a ProvisioningEngine I had developed based on Ruby on Rails and java (Apache Camel). The advantages of BDD I see are:

  • better customer view: if you follow the behavior driven principle, your first thought is, how the web pages look like and how the web pages behave with respect to customer actions — in detail. This helps me to always start with the customer view in mind.
  • higher motivation: as a developer, I find it rewarding to start with test development with “red” test cases that become green over time
  • higher quality: I often challenge myself to optimize my code (e.g. make it DRYer of more versatile). In this process, I do not want to sacrifice and previous achievements. A large set of unit tests and e2e test help me to keep the set of features intact in phases of code restructuring

Okay, as an Angular beginner, I deem I am far from being an ideal behavior driven Angular developer. However, at some point in future, I believe that I can increase my hobby development productivity by applying principles like BDD together with build&deployment automation based on TravisCI, CircleCI or a local Jenkins system to my development approach.

Overview

Along the way, we will get acquainted with a set of typical error messages and we will learn how to cope with them.

So, if you are ready for a quick ride into a simple “test first” strategy example with GIT repo handling, buckle up and start coding with me in four phases:

😉

  • Phase 1: Create a Hello World App based on Angular CLI
  • Phase 2: Adapt the end-to-end Tests
  • Phase 3: Adapt the Code
  • Phase 4: Verify the successful e2e Tests

If you do not care about BDD and GIT, then you might also want head over to the post Consuming a RESTful Web Service with Angular 4. Or better, follow the instructions you find here, but omit the steps related to e2e testing (protractor) and/or GIT.

Phase 1: Create a Hello World App based on Angular CLI

In this phase, we will

  • use an Angular CLI Docker image to create a new application
  • fix some problems with the end to end testing inherent in the standard hello world app
  • save and upload the changes to GIT

Step 1.0: Get access to a Docker Host with enough Resources

If you do not have access to a Docker host yet, I recommend following the step 0 instructions on my JHipster post. I recommend to use a Docker host with at least 1.5 GB RAM. To be honest, this is a guess. I always test on a 4 GB Docker Host Virtualbox VM, but I know that 750 MB RAM is not sufficient.

Step 1.1: Prepare an alias for later use

Let us first define an alias that helps us to shorten the commands thereafter.

(dockerhost)$ alias cli='docker run -it --rm -w /app -v $(pwd):/app -p 4200:4200 -u $(id -u $(whoami)) oveits/angular-cli:1.4.3 $@'

Why this complicated user option -u $(id -u $(whoami))? The reason is that

  • if we omit it, then all new files will be created as root, so we will get permissions problems later on
  • If we use ‘centos’, then the container will complain that he does not find the user ‘centos’ in its passwd file
  • If we use the ID of centos, then it works. However, it might not work in all cases. This time, the ID of centos user is 1000, and by chance, a user (named ‘node’) exists on the container as well. But let us live with this uncertainty for now.

With each cli something command, we will start a something command on an Angular CLI @ Alpine container originally created by Alex Such and enriched with git and bash by me.

Consider appending the alias command to your Docker host’s ~/.bashrc file, so the alias is persistent.

Step 1.2: Create a Project and install required Modules

Now let us create a new project and install the node modules via npm:

(dockerhost)$ cli ng new consuming-a-restful-web-service-with-angular
(dockerhost)$ cd consuming-a-restful-web-service-with-angular
(dockerhost)$ cli npm install
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.11.2
npm info attempt registry request try #1 at 7:54:24 PM
npm http request GET https://registry.npmjs.org/fsevents
npm http 200 https://registry.npmjs.org/fsevents
npm info lifecycle consuming-a-restful-web-service-with-angular@0.0.0~preinstall: consuming-a-restful-web-service-with-angular@0.0.0
npm info linkStuff consuming-a-restful-web-service-with-angular@0.0.0
npm info lifecycle consuming-a-restful-web-service-with-angular@0.0.0~install: consuming-a-restful-web-service-with-angular@0.0.0
npm info lifecycle consuming-a-restful-web-service-with-angular@0.0.0~postinstall: consuming-a-restful-web-service-with-angular@0.0.0
npm info lifecycle consuming-a-restful-web-service-with-angular@0.0.0~prepublish: consuming-a-restful-web-service-with-angular@0.0.0
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm info ok

Step 1.3 (optional): Create a local GIT Repository

Now is a good time to create a git repository and to commit the initial code.

If you have not installed GIT on your Docker host, depending on the operating system of your Docker host, you might need to install it first (e.g. apt-get update; apt-get install -y git in case of Ubuntu, or yum install -y git in case of CentOS). Alternatively, you may want to use the git I have installed in the container. In that case, prepend a “do” before the git command, e.g. try cli git --version. However, a git diff does not look nice in a container, so I recommend to install GIT on your Docker host instead.

Now let us initialize the git repo, add all files and commit the changes:

(dockerhost)$ git init
(dockerhost)$ git add .
(dockerhost)$ git commit -m'initial commit'

Now let us start the service in a container:

(dockerhost)$ cli ng serve --host 0.0.0.0
** NG Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **
Date: 2017-09-26T20:04:45.036Z
Hash: 24fe32460222f3b3faf2
Time: 15376ms
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered]
chunk {main} main.bundle.js, main.bundle.js.map (main) 8.88 kB {vendor} [initial] [rendered]
chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 209 kB {inline} [initial] [rendered]
chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 11.3 kB {inline} [initial] [rendered]
chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.29 MB [initial] [rendered]

webpack: Compiled successfully.

Step 1.4: Perform end-to-end Tests

Step 1.4.1: Use a Protractor Docker Image to perform the Tests

In the spirit of “test first” strategies of “behavior-driven development”, let us check the end-to-end tests that come with Angular CLI 1.4.3. We will see that they are broken and need to be adapted.

Like above, we will use a Docker container for the task. This time we will use the Docker image protractor-headless from webnicer. In a second terminal, we first define an alias, enter the project root folder and run protractor.

(dockerhost)$ alias protractor='docker run -it --privileged --rm --net=host -v /dev/shm:/dev/shm -v $(pwd):/protractor webnicer/protractor-headless $@'
(dockerhost)$ cd consuming-a-restful-web-service-with-angular
(dockerhost)$ protractor

[20:20:34] I/direct - Using ChromeDriver directly...
[20:20:34] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✗ should display welcome message
      - Failed: Error while waiting for Protractor to sync with the page: "Could not find testability for element."
          at /usr/local/lib/node_modules/protractor/built/browser.js:272:23
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)Error
          at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/built/element.js:461:27)
          at ElementArrayFinder._this.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:103:30)
          at ElementFinder.(anonymous function) [as getText] (/usr/local/lib/node_modules/protractor/built/element.js:829:22)
          at AppPage.getParagraphText (/protractor/e2e/app.po.ts:9:43)
          at Object. (/protractor/e2e/app.e2e-spec.ts:12:17)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
      From: Task: Run it("should display welcome message") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:16:5
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:10:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:3:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:392:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:395:12)

**************************************************
*                    Failures                    *
**************************************************

1) consuming-a-restful-web-service-with-angular App should display welcome message
  - Failed: Error while waiting for Protractor to sync with the page: "Could not find testability for element."

Executed 1 of 1 spec (1 FAILED) in 0.878 sec.
[20:20:41] I/launcher - 0 instance(s) of WebDriver still running
[20:20:41] I/launcher - chrome #01 failed 1 test(s)
[20:20:41] I/launcher - overall: 1 failed spec(s)
[20:20:41] E/launcher - Process exited with error code 1

Even though my application is listening on port 4200,  we can see that the e2e tests have a problem.

Step 1.4.2: Correct the Protractor sync Issue

As already pointed out in this blog post, we need to add the option

useAllAngular2AppRoots: true

to our protractor.conf.js file. At the end, the file has following content (correction in blue):

// protractor.conf.js
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  useAllAngular2AppRoots: true,
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

After that, the e2e test is still not successful:

$ protractor
[20:30:32] I/direct - Using ChromeDriver directly...
[20:30:32] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✗ should display welcome message
      - Expected 'Welcome to !' to equal 'Welcome to app!'.
          at Object. (/protractor/e2e/app.e2e-spec.ts:12:37)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)

**************************************************
*                    Failures                    *
**************************************************

1) consuming-a-restful-web-service-with-angular App should display welcome message
  - Expected 'Welcome to !' to equal 'Welcome to app!'.

Executed 1 of 1 spec (1 FAILED) in 0.848 sec.
[20:30:40] I/launcher - 0 instance(s) of WebDriver still running
[20:30:40] I/launcher - chrome #01 failed 1 test(s)
[20:30:40] I/launcher - overall: 1 failed spec(s)
[20:30:40] E/launcher - Process exited with error code 1

Step 1.4.3: Correct the e2e Test Script

The reason is that the app.component.ts file is not correct. In the HTML template, we find a line

Welcome to {{title}}!

but in the component file, the title is missing:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

This is leading to following corrupt web page:

Let us correct this now (in blue):

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  title : any = null

  constructor() { }

  ngOnInit() {
     this.title = "app";
  }

}

Now the Web page looks better:

Now the e2e tests are successful:

$ protractor
[20:53:42] I/direct - Using ChromeDriver directly...
[20:53:42] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display welcome message

Executed 1 of 1 spec SUCCESS in 0.956 sec.
[20:53:50] I/launcher - 0 instance(s) of WebDriver still running
[20:53:50] I/launcher - chrome #01 passed

The angular CLI installation works as expected now.

Let us save the changes:

(dockerhost)$ git add protractor.conf.js
(dockerhost)$ git commit -m'protractor.conf.js: added useAllAngular2AppRoots: true for avoiding sync problems'
(dockerhost)$ git add src/app/app.component.ts
(dockerhost)$ git commit -m'app component: defined missing title'

Now is the time to sign up with Github and save the project. In my case, I have created following project: a project like follows:

https://github.com/oveits/consuming-a-restful-web-service-with-angular

Once this is done, we can upload the changes as follows:

(dockerhost)$ git remote add origin https://github.com/oveits/consuming-a-restful-web-service-with-angular.git
(dockerhost)$ git push -u origin master

Phase 2: Adapt the end-to-end Tests

In this phase, we will

  • based on the input from the WordPress API, we will plan, how the web page should look like from a customer’s point of view.
  • We will adapt the e2e tests, so they reflect the (assumed) customer’s expectations.
  • We will save the changed code to the remote GIT repository.

Step 2.1: Planning

In an attempt to follow a behavior driven development process, we will write/adapt the end to end tests first, before we perform the changes. For this, let us outline our plan:

  • We would like to create a Web page that displays the title and content of a WordPress Article
  • the WordPress article of our choice is the first angular blog post I have written: the Angular 4 Hello World Quickstart blog post
  • The article will be retrieved dynamically from the WordPress API, a REST API.

Step 2.2: Explore the WordPress REST API

Let us have a look at the WordPress API. The WordPress API can be explored via the WordPress.com REST API console. We can display a list of blog posts like so:

We can see that the blog post we would like to display has the ID 3078 and the title and content star like follows:

  • title: “Angular 4 Hello World Quickstart”
  • content: “<p>In this hello world style tutorial, we will follow a step by step guide to a working Angular 4 application. We will also …

The  single blog post can be retrieved with the URL

https://public-api.wordpress.com/rest/v1.1/sites/oliverveits.wordpress.com/posts/3078

We can verify this by copying the URL into a Browser:

Step 2.3: Adapt the end-to-end Tests

With the knowledge about the title and content of the blog post, we can re-write the end-to-end (e2e) test. The e2e test is found in the e2e folder:

ls e2e/
app.e2e-spec.ts app.po.ts tsconfig.e2e.json

$ cat e2e/app.e2e-spec.ts
import { AppPage } from './app.po';

describe('consuming-a-restful-web-service-with-angular App', () => {
  let page: AppPage;

  beforeEach(() => {
    page = new AppPage();
  });

  it('should display welcome message', () => {
    page.navigateTo();
    expect(page.getParagraphText()).toEqual('Welcome to app!');
  });
});

Instead of searching for the text ‘Welcome to app’, let us search for the title “Angular 4 Hello World Quickstart”:

$ cat e2e/app.e2e-spec.ts
import { AppPage } from './app.po';

describe('consuming-a-restful-web-service-with-angular App', () => {
  let page: AppPage;

  beforeEach(() => {
    page = new AppPage();
  });

  it('should display the title', () => {
    page.navigateTo();
    expect(page.getParagraphText()).toContain('Angular 4 Hello World Quickstart');
  });
});

The e2e test should fail now with the message Expected 'Welcome to app!' to contain 'Angular 4 Hello World Quickstart'

$ protractor
[20:46:02] I/direct - Using ChromeDriver directly...
[20:46:02] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✗ should display welcome message
      - Expected 'Welcome to app!' to contain 'Angular 4 Hello World Quickstart'.
          at Object. (/protractor/e2e/app.e2e-spec.ts:12:37)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
          at new ManagedPromise (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
          at controlFlowExecute (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)

**************************************************
*                    Failures                    *
**************************************************

1) consuming-a-restful-web-service-with-angular App should display welcome message
  - Expected 'Welcome to app!' to contain 'Angular 4 Hello World Quickstart'.

Executed 1 of 1 spec (1 FAILED) in 0.907 sec.
[20:46:21] I/launcher - 0 instance(s) of WebDriver still running
[20:46:21] I/launcher - chrome #01 failed 1 test(s)
[20:46:21] I/launcher - overall: 1 failed spec(s)
[20:46:21] E/launcher - Process exited with error code 1

Step 2.4: Save the Changes on a separate GIT Branch

We believe that the e2e tests are correct now, so it is a good time to create a new git feature branch and commit the code:

git checkout -b feature/0001-retrieve-and-display-WordPress-title-from-API
git add .
git commit -m'adapted e2e tests to display WordPress blog title'
git push

Phase 3: Adapt the Code

Now, after having written the e2e tests, let us change the code, so our app fulfills the expectations.

Step 3.1: Define the HTML View

In the spirit of a behavior driven approach, let us define the view first. For that we replace the content of the app’s template file:

$ cat src/app/app.component.html
<h1>{{title}}</h1>

The output of the application now is:

This is because, in the Hello World app, we have set the title to the static value ‘app’. The e2e tests are not successful and the error ‘Expected ‘app’ to contain ‘Angular 4 Hello World Quickstart’.’ is thrown when we run protractor.

Step 3.2: Subscribe an Observable

As can be seen in many tutorials, we now subscribe to an observable like follows:

$ cat src/app/app.component.ts
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  title : any = null

  constructor() { }

  ngOnInit() {
     //this.title = "app";
     this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/oliverveits.wordpress.com/posts/3078')
                .map((res: Response) => res.json())
                 .subscribe(data => {
                        this.title = data.title;
                        console.log(data);
                });
  }

}

We perform an HTTP GET on the WordPress API’s URL, map the response to a JSON object and subscribe the retrieved data. The data should contain a title, which we assign to the local title variable.

However, we will see in the log:

ERROR in /app/src/app/app.component.ts (16,11): Property '_http' does not exist on type 'AppComponent'.

And in the browser, we see:

Let us fix that now.

Step 3.3: Define private local _http Variable

In angular, we can define the private local _http variable in the constructor:

constructor(private _http: Http) {}

Once, this is done, the error message is changed to:

ERROR in /app/src/app/app.component.ts (12,30): Cannot find name 'Http'.

Step 3.4: Import Http Components

The used Http module is not known to our app component. Let us change this now. We add the following line

import { Http } from '@angular/http';

to the file src/app/app.component.ts. The error message changes to:

ERROR in /app/src/app/app.component.ts (18,18): Property 'map' does not exist on type 'Observable<Response>'.

Step 3.5: Import map

The map function needs to be imported as well:

import 'rxjs/add/operator/map'

Now we get an illegible error like follows:

ERROR in /app/src/app/app.component.ts (18,6): The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
  Type argument candidate 'Response' is not a valid type argument because it is not a supertype of candidate 'Response'.
    Types of property 'type' are incompatible.
      Type 'ResponseType' is not assignable to type 'ResponseType'. Two different types with this name exist, but they are unrelated.
        Type '"basic"' is not assignable to type 'ResponseType'.

Step 3.6: Import Response Type

We finally can get rid of the quite illegible error message by adding another import:

import { Response } from '@angular/http';

However, this still does not lead to the desired result. In the browser we see an empty page:

and the e2e tests fail with the following message:

$ protractor
...
Failed: Angular could not be found on the page http://localhost:4200/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load

Step 3.7: Add HttpModule in the app Module

The solution of the above error lies in the src/app/app.module.ts (added parts in blue). We first need to add the HttpModule to the imports, which alters the error message to

$ cat src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule }    from '@angular/http';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

This seems to have been the last stepping stone towards success:

Phase 4: Verify the successful e2e Tests

Now the e2e tests are successful:

$ protractor
[22:16:06] I/direct - Using ChromeDriver directly...
[22:16:06] I/launcher - Running 1 instances of WebDriver
Jasmine started

  consuming-a-restful-web-service-with-angular App
    ✓ should display welcome message

Executed 1 of 1 spec SUCCESS in 1 sec.
[22:16:14] I/launcher - 0 instance(s) of WebDriver still running
[22:16:14] I/launcher - chrome #01 passed

That is, how the e2e Tests should look like. Success!

Step 4.2: Save the changes to the develop branch on GIT

Since our new feature “retrieve and display a blog title from WordPress API” has been verified to work fine, it is time to commit the change and push it to the remote repository:

git add .
git commit -m'added all code needed for successful e2e tests'
git push
git checkout -b develop
git push

In addition to that, we can create a new “develop” branch, if it does not exist yet:

git checkout -b develop
git push

In case the develop branch exist already, you need to merge the code to the instead of creating the develop branch:

git checkout develop
git merge feature/0001-retrieve-and-display-WordPress-title-from-API
git push

It makes sense to allow a merge to the develop branch only if the code is fully tested. This way, we will never break the code in the develop branch.

For large teams, several measures can be taken to make sure that only high quality code enters the develop branch: e.g. on BitBucket GIT, you can allow a merge only, if code has been reviewed and acknowledged by a certain number of team members. Moreover, you can integrate the repository with a Jenkins system: with the correct plugins, you can make sure that a merge is allowed only in case all quality gates (e2e test, unit tests, style, performance, …) in the Jenkins pipeline are met.

However, if you are a hobby developer working on a , it is probably sufficient if you run the tests manually before you merge the changed code into the develop or master branch.

Summary

In this hello world style step-by-step guide, we have learned

  • How to create a new Hello World project using Angular CLI, repair the e2e tests and save the changes on GIT.
  • How to create/adapt the e2e tests in advance a “test first” manner.
  • How to consume a REST service using Angular 4 and verify the result using the e2e test scripts we have created before.

Next Steps

In part 2 of this series, we will learn how to add and display HTML content to the body of our application. We will see that we cannot just use the method we have used for the title. If we do so, we will see escaped HTML code like follows:

<p>In this hello world style tutorial,…

We will show how to make Angular accept the HTML code and display it correctly.

References:

Appendix A: Adding Docker Support

This is, how I have added Docker support for the application, following my tl:dr of the blog post Angular 4 Docker Example.

A.1 Add Dockerfile and NginX config

git clone https://github.com/oveits/consuming-a-restful-web-service-with-angular
git checkout -b feature/0002-docker-support
curl -O https://raw.githubusercontent.com/avatsaev/angular4-docker-example/master/Dockerfile
curl -O https://raw.githubusercontent.com/avatsaev/angular4-docker-example/master/nginx/default.conf
mkdir nginx
mv default.conf nginx/

remove ‘package-log.json’ from Dockerfile

git add .
git commit -m 'added Dockerfile and nginx config file'
git push

A.2 Build the Docker  Image

On a docker host, I have issued following commands:

docker build . --tag oveits/consuming-a-restful-web-service-with-angular:v0.2
docker push oveits/consuming-a-restful-web-service-with-angular:v0.2
docker tag oveits/consuming-a-restful-web-service-with-angular:v0.2 oveits/consuming-a-restful-web-service-with-angular:latest
docker push oveits/consuming-a-restful-web-service-with-angular:v0.2
docker push oveits/consuming-a-restful-web-service-with-angular:latest

A.3 Run the Service

$ alias consuming='docker run --rm --name consuming-a-restful-web-service-with-angular -d -p 80:80 oveits/consuming-a-restful-web-service-with-angular $@'
$ consuming

A.4 Access the Service

In a browser, head to the public DNS of the image:

Works!

 

 

0

Angular end to end Testing

– Angular e2e Protractor Tests on Systems without GUI applied to static and dynamic Web Pages –

In this blog post, we will show how to perform end-to-end (e2e) tests with Angular 4:

  • first, we will apply Protractor end to end tests on a little Angular CLI hello world application with static HTML content
  • second, we will perform end to end tests for a dynamic web application, a REST client application that retrieves and displays content received via the WordPress API.

For that, we will use a Docker Protractor Image that can be used on systems with or without graphical user interface (e.g. a Jenkins CI system).

 

In a classical installation situation, you often use a machine with a graphical window system (e.g. X11 for Linux) so you can run a real Chrome or Firefox Browser on the system. In such a situation, you often install Jasmine, Protractor, Selenium and a Chrome or Firefox browser on the test machine. However, in this blog post, we prefer to use a pre-installed Docker image provided by webnicer instead, which allows us to run the e2e tests on Docker hosts without a graphical system. The missing need for a graphical interface makes this an ideal deployment option for continuous integration purposes, e.g. for Jenkins systems.

Why End to End Tests?

My motivation to write a blog about end-to-end tests is, that I have made a very good experience with “tests first” and more specifically “behavior driven development” (BDD) principles with previous projects. And end-to-end tests are the main ingredient needed for BDD. In my experience, projects that follow “test first” or BDD principles benefit from a better customer view and higher quality, together with higher motivated developers, who start their work with a set of failed (red) tests and are rewarded for their work with successful (green) tests.

Even if we are far from following behavior driven principles yet, let us perform our first tiny steps towards this principle by looking more closely at end-to-end testing now:

Step 1: Install and start an Angular CLI Hello World Application

We are closely following the phase 1 instructions on a previous popular blog post of mine Consuming a RESTful Web Service with Angular 4. Since I do not want to copy and paste that part of the other blog post, let me just summarize the commands after you have got access to a Docker host (e.g. by following the instructions found here; search for the term “Install a Docker Host”):

  • start Docker container
docker run -it -p 4200:4200 -v $(pwd):/localdir centos bash
  • install Angular CLI
(container)# yum install -y epel-release
(container)# yum install -y https://kojipkgs.fedoraproject.org//packages/http-parser/2.7.1/3.el7/x86_64/http-parser-2.7.1-3.el7.x86_64.rpm
(container)# yum install -y nodejs 
(container)# npm install -g @angular/cli
  • create a project
(container)# cd /localdir
(container)# ng new my-project-name
(container)# cd my-project-name
  • we do not yet start the service in order to see a certain error message, but in step 3, we will start the service with this command:
(container)# ng serve --host 0.0.0.0

For more detailed information about Step 1, see phase 1 of this blog post.

Step 2: Prepare Docker Host for Protractor Usage

For a convenient handling of the protractor test, let us open a new terminal and create a shell script on the Docker host like follows (see the readme of the Docker image page webnicer/protractor-headless):

If your Docker host does not allow the usage of sudo, then try the same commands without sudo.

(dockerhost)$ sudo cat - << END | sudo tee /usr/local/bin/protractor-headless
#!/bin/bash
docker run -it --privileged --rm --net=host -v /dev/shm:/dev/shm -v \$(pwd):/protractor webnicer/protractor-headless \$@
END

Then we make sure the file is executable:

(dockerhost)$ sudo chmod +x /usr/local/bin/protractor-headless
(dockerhost)$ which /usr/local/bin/protractor-headless
/usr/local/bin/protractor-headless

Step 3: Download and run Protractor in a Docker Container

Let us download the protractor Docker image webnicer/protractor-headless so the container will be started immediately from the image later:

(dockerhost)$ docker pull webnicer/protractor-headless 

Then we enter the project root folder we have created in step 1:

(dockerhost)$ cd my-project-name

and run protractor via:

(dockerhost)$ protractor-headless
[23:43:08] I/direct - Using ChromeDriver directly...
[23:43:08] I/launcher - Running 1 instances of WebDriver
Spec started
[23:43:24] E/protractor - Could not find Angular on page http://localhost:4200/ : retries looking for angular exceeded

  my-project-name App
    ✗ should display welcome message
      - Failed: Angular could not be found on the page http://localhost:4200/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
          at /usr/local/lib/node_modules/protractor/built/browser.js:506:23
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)
      From: Task: Run it("should display welcome message") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:16:5
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:10:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:3:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:382:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:385:12)

**************************************************
*                    Failures                    *
**************************************************

1) my-project-name App should display welcome message
  - Failed: Angular could not be found on the page http://localhost:4200/. If this is not an Angular application, you may need to turn off waiting for Angular. Please see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load

Executed 1 of 1 spec (1 FAILED) in 10 secs.
[23:43:24] I/launcher - 0 instance(s) of WebDriver still running
[23:43:24] I/launcher - chrome #01 failed 1 test(s)
[23:43:24] I/launcher - overall: 1 failed spec(s)
[23:43:24] E/launcher - Process exited with error code 1

Okay, this is expected, because no angular is running on port 4200 yet.

Step 4: Start Angular CLI Application and run Protractor again

As anticipated, let us start the Angular application as describes in step 1:

(container)# ng serve --host 0.0.0.0

Then we get following output, if we try to run the e2e tests:

(dockerhost)$ protractor-headless
[00:19:21] I/direct - Using ChromeDriver directly...
[00:19:21] I/launcher - Running 1 instances of WebDriver
Spec started

  my-project-name App
    ✗ should display welcome message
      - Failed: Error: Error while waiting for Protractor to sync with the page: "Could not find testability for element."
          at proxyDone.fail (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:87:34)
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)
      From: Task: Run it("should display welcome message") in control flow
          at Object. (/usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
          at /usr/local/lib/node_modules/protractor/node_modules/jasminewd2/index.js:16:5
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
      From asynchronous test:
      Error
          at Suite. (/protractor/e2e/app.e2e-spec.ts:10:3)
          at Object. (/protractor/e2e/app.e2e-spec.ts:3:1)
          at Module._compile (module.js:570:32)
          at Module.m._compile (/protractor/node_modules/ts-node/src/index.ts:382:23)
          at Module._extensions..js (module.js:579:10)
          at Object.require.extensions.(anonymous function) [as .ts] (/protractor/node_modules/ts-node/src/index.ts:385:12)

**************************************************
*                    Failures                    *
**************************************************

1) my-project-name App should display welcome message
  - Failed: Error: Error while waiting for Protractor to sync with the page: "Could not find testability for element."

Executed 1 of 1 spec (1 FAILED) in 0.565 sec.
[00:19:26] I/launcher - 0 instance(s) of WebDriver still running
[00:19:26] I/launcher - chrome #01 failed 1 test(s)
[00:19:26] I/launcher - overall: 1 failed spec(s)
[00:19:26] E/launcher - Process exited with error code 1

Step 5: Fix the “waiting for Protractor to sync” Problem and run Protractor again

I have found a solution on this StackOverflow Q&A: we need to add the additional configuration line into protractor.conf.js:

useAllAngular2AppRoots: true

In my case, the protractor configuration file looks like follows:

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  useAllAngular2AppRoots: true,
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

After that, the e2e test via Protractor Docker Container is successful:

protractor-headless
[00:46:32] I/direct - Using ChromeDriver directly...
[00:46:32] I/launcher - Running 1 instances of WebDriver
Spec started
[00:46:38] W/element - more than one element found for locator By(css selector, app-root h1) - the first result will be used

  my-project-name App
    ✓ should display welcme message

Executed 1 of 1 spec SUCCESS in 1 sec.
[00:46:38] I/launcher - 0 instance(s) of WebDriver still running
[00:46:38] I/launcher - chrome #01 passed

Step 6: Review the Protractor Spec File

Now, we want to understand in more detail, what happened. For that, let us analyze the e2e specification file (e2e/app.e2e-spec.ts):

import { MyProjectNamePage } from './app.po';

describe('my-project-name App', () => {
  let page: MyProjectNamePage;

  beforeEach(() => {
    page = new MyProjectNamePage();
  });

  it('should display welcome message', done => {
    page.navigateTo();
    page.getParagraphText()
      .then(msg => expect(msg).toEqual('Welcome to app!!'))
      .then(done, done.fail);
  });
});

Now let us compare that with the browser content on http://localhost:4200:

There, it is: the e2e test is loading the page, retrieving the first paragraph and will compare it with the text “Welcome to app!!”. Since the text of the first paragraph matches this text, the test is successful.

Step 7 (optional): Review the Error Message of a failed Test

Now let us see, what happens, if we change the expected text in e2e/app.e2e-spec.ts to “Welcome to ppa!!” instead:

$ protractor-headless
[00:32:30] I/direct - Using ChromeDriver directly...
[00:32:30] I/launcher - Running 1 instances of WebDriver
Spec started
[00:32:36] W/element - more than one element found for locator By(css selector, app-root h1) - the first result will be used

  my-project-name App
    ✗ should display welcome message
      - Expected 'Angular 4 Hello World Quickstart' to equal 'Welcome to ppa!!'.
          at /protractor/e2e/app.e2e-spec.ts:13:32
          at /usr/local/lib/node_modules/protractor/built/element.js:798:32
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)

**************************************************
*                    Failures                    *
**************************************************

1) my-project-name App should display welcome message
  - Expected 'Angular 4 Hello World Quickstart' to equal 'Welcome to ppa!!'.

Executed 1 of 1 spec (1 FAILED) in 1 sec.
[00:32:36] I/launcher - 0 instance(s) of WebDriver still running
[00:32:36] I/launcher - chrome #01 failed 1 test(s)
[00:32:36] I/launcher - overall: 1 failed spec(s)
[00:32:36] E/launcher - Process exited with error code 1

So, this is, how a failed test looks like.

Step 8 (advanced): Apply the e2e Test to a dynamic Web Page

Up to now, we have seen a simple test, which is comparing a static pattern with a static web page. We now want to apply the principle to a dynamic page as we have created in my blog post Consuming a RESTful Web Service with Angular 4. For that, we reverse the change in Step 7, so we get a successful e2e test again. Then we need to follow the steps in the corresponding blog post and run the resulting service on localhost port 4200. After that, the e2e test will fail:

(dockerhost)$ protractor-headless
[00:32:30] I/direct - Using ChromeDriver directly...
[00:32:30] I/launcher - Running 1 instances of WebDriver
Spec started
[00:32:36] W/element - more than one element found for locator By(css selector, app-root h1) - the first result will be used

  my-project-name App
    ✗ should display welcome message
      - Expected 'Angular 4 Hello World Quickstart' to equal 'Welcome to app!!'.
          at /protractor/e2e/app.e2e-spec.ts:13:32
          at /usr/local/lib/node_modules/protractor/built/element.js:798:32
          at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
          at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
          at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
          at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27)
          at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
          at process._tickCallback (internal/process/next_tick.js:103:7)

**************************************************
*                    Failures                    *
**************************************************

1) my-project-name App should display welcome message
  - Expected 'Angular 4 Hello World Quickstart' to equal 'Welcome to app!!'.

Executed 1 of 1 spec (1 FAILED) in 1 sec.
[00:32:36] I/launcher - 0 instance(s) of WebDriver still running
[00:32:36] I/launcher - chrome #01 failed 1 test(s)
[00:32:36] I/launcher - overall: 1 failed spec(s)
[00:32:36] E/launcher - Process exited with error code 1

But what is the expected result we want to see? Let us head to http://localhost:4200 again, and we will see:

The blue part is static content again. However, the Title “Angular 4 Hello World Quickstart” is dynamic content retrieved from the WordPress API. We can easily see that this pattern is missing in the source HTML code:

Nevertheless, we want to use our Protractor Docker Container to test, whether the dynamic content is visible in a browser. For that, let us adapt the e2e test file on (e2e/app.e2e-spec.ts) to the new situation:

import { MyProjectNamePage } from './app.po';

describe('my-project-name App', () => {
  let page: MyProjectNamePage;

  beforeEach(() => {
    page = new MyProjectNamePage();
  });

  it('should display title', done => {
    page.navigateTo();
    page.getParagraphText()
      .then(msg => expect(msg).toContain('Angular 4 Hello World Quickstart'))
      .then(done, done.fail);
  });
});

the e2e test is successful:

(dockerhost)$ protractor-headless
[00:46:32] I/direct - Using ChromeDriver directly...
[00:46:32] I/launcher - Running 1 instances of WebDriver
Spec started
[00:46:38] W/element - more than one element found for locator By(css selector, app-root h1) - the first result will be used

  my-project-name App
    ✓ should display title

Executed 1 of 1 spec SUCCESS in 1 sec.
[00:46:38] I/launcher - 0 instance(s) of WebDriver still running
[00:46:38] I/launcher - chrome #01 passed

We have achieved our goal: we had intended to perform a successful end to end test for a dynamic web page that retrieves its content from an external resource like the WordPress API.

Summary

In this blog post, we

  • have applied a Protractor Docker Container on an existing Angular CLI Hello World.
    For this to work, we had to adapt the Protractor configuration file to circumvent an “Error while waiting for Protractor to sync with the page”
  • have applied a Protractor Docker Container with the patched Protractor configuration on an app that is dynamically downloading and displaying content retrieved from the WordPress API. This has worked as expected although the content we have looked for was not visible in the HTML source code.

With the latter, we have successfully verified that the Protractor Docker container can handle dynamic content retrieved and altered via javascript.

Next Steps

  • Write more complete test cases for the dynamic web page
  • Refactor the dynamic web page application by dividing the HTTP GET into a separate service. The Protractor tests will help me to verify that the refactored application will keep its previously achieved features.

References:

0

Hello Java Hipster: Angular 4 and Spring Boot

In this blog post, Java Hipster will help us creating a mini blog application based on Angular 4 and Spring Boot. Angular is a popular framework for creating reactive single page applications, while Spring Boot is a robust java-based backend framework that helps you create database access and RESTful APIs.

We will closely follow the JHipster introduction on YouTube and explore administrative functions like user management, logging and API management (with swagger). After creating an importing a simple model for a blog, we will manipulate the blog and its entries via the GUI.

For your convenience, we will run everything in a Docker container, so you do not need to install any of the required software packages (apart from Docker itself). As a little goody, we will outline how to add some additional security functions: we will restrict delete access to authorized users. That is an extension to the many good examples shown in the JHipster introduction on YouTube video.

Tools and Versions used

  • Vagrant 1.8.6
  • Virtualbox 5.0.20 r106931
  • jhipster Docker image v4.6.2

Why JHipster?

JHipster is a Yeoman-based code generator for a complete Web application based on Angular and Spring Boot, two popular frameworks for front-end and backend systems.

  • Angular is a popular single page frontend framework
  • Spring Boot is a high-performance backend framework

mixed with CSS Bootstrap styles, and an easy-to use web-based modeling tool that creates text-based models that can be used to generate the corresponding database and REST entities/functions, JHipster, JHipster is a quick way to get projects started.

I have not checked out yet all other features mentioned on their home page, like

  • microservice support with JHipster Registry, Netflix OSS, Elastic stack and Docker

We will build our first application within a Docker container.

Step 0: Install a Docker Host

This time (again), our application will need more than 750 MB RAM. Therefore, we cannot use my beloved Katacoda as our Docker playground, which is limited to this amount of DRAM. Instead, you need to get access to a Docker host with, >~ 2 GB RAM. A nice way of installing an Ubuntu Docker host via Vagrant is described here (search for the term “Install a Docker Host”).

Prerequisites of this step:

  • I recommend to have direct access to the Internet: via Firewall, but without HTTP proxy. However, if you cannot get rid of your HTTP proxy, read this blog post.
  • Administration rights on you computer.

Steps to install a Docker Host VirtualBox VM:

Download and install Virtualbox (if the installation fails with error message “Oracle VM Virtualbox x.x.x Setup Wizard ended prematurely” see Appendix A of this blog post: Virtualbox Installation Workaround below)

1. Download and Install Vagrant (requires a reboot)

2. Download Vagrant Box containing an Ubuntu-based Docker Host and create a VirtualBox VM like follows:

(basesystem)# mkdir ubuntu-trusty64-docker ; cd ubuntu-trusty64-docker
(basesystem)# vagrant init williamyeh/ubuntu-trusty64-docker
(basesystem)# vagrant up
(basesystem)# vagrant ssh

Now you are logged into the Docker host and we are ready for the next step: to download the Docker image and to start the application in a container.

Step 1: Start JHipster Docker Container

(dockerhost)$ docker run -it -p 8080:8080 -p 3001:3001 -p 9000:9000 -p 9060:9060 -v $(pwd):/localdir jhipster/jhipster:v4.6.2 bash
(containter)# cd /localdir

Note: You also can omit the version tag (:v4.6.2) and work with the latest image. However, v4.6.2 is the version tested in this blog post.

Note that you might have to view (docker ps) and stop (docker stop) any container that is running on port 8080 first, since I have experienced problems, when mapping the port 8080 to another port than 8080 (I need to try again). If you have followed step 0, you will need to issue the command

(dockerhost) sudo docker stop cadvisor

Step 2: Create, Start and Login to App

Step 2.1: Create App

(container)# mkdir blog; cd blog
(container)# yo jhipster

I have chosen

  • monolithic
  • base name: blog
  • default java package name: org.jhipster <– should use another name, probably
  • authentication: JWT
  • database type: SQL
  • production database: MySQL
  • development database: H2 with disk-based persistence
  • Hibernate 2nd level cache: Yes, with ehcache (local cache, for a single node)
  • Other Technologies (choose none, just press enter)
  • Framework: Angular 2
  • libSass: Yes
  • Internationalization: Yes
  • Chosen English and German
  • Testing Frameworks (bBesides JUnit and Karma:
    • Gatling
    • Protractor

Wait for ~2-4 minutes

Step 2.2 Start App

In order to be able to explore an (empty) JHipster app, we can start the application as follows:

Start Spring Boot Application:

In the project root directory, type:

(container)# ./mvnw

Only as a reference: Start Webpack development server (was not needed in my case!)

container)# yarn start

Step 2.3 (optional): Connect to (default) App

In a local browser, connect to localhost:8080:

Sign in as admin with password “admin”.

Step 2.4 (optional): Explore (default) App

The Entities are empty yet, but unser Administration we can see the user management, metrics etc:

We have not yet created and imported a model, so this is still the default JHipster app. It comes with a User Model, though, with three users created per default:

  • admin / admin
  • user / user
  • system / system?

Under Administration -> Metrics, we can see JVM Metrics as well as HTTP statistics:

Under Administration -> Configuration, we see the Spring properties, even though they seem to be read-only:

However, the log configuration can be edited in the way that a log can be chosen e.g. to show DEBUG instead of WARN messages only

Under Administration -> API we find a swagger page that helps you to test your REST services:

Okay, I am cheating a little here: you will not be able to see the blog-resource yet, since we will create it not before the next two steps. You will only see the account, the profile info, the user-jwt controller and the user resource. It can be used to test the interface. E.g. you can read all users by clicking the button

of users-resource GET /api/users and we will receive something like follows:

[
  {
    "id": 1,
    "login": "system",
    "firstName": "System",
    "lastName": "System",
    "email": "system@localhost",
    "imageUrl": "",
    "activated": true,
    "langKey": "en",
    "createdBy": "system",
    "createdDate": "2017-08-15T11:47:06.180Z",
    "lastModifiedBy": "system",
    "lastModifiedDate": null,
    "authorities": [
      "ROLE_USER",
      "ROLE_ADMIN"
    ]
  },
  {
    "id": 3,
    "login": "admin",
    "firstName": "Administrator",
    "lastName": "Administrator",
    "email": "admin@localhost",
    "imageUrl": "",
    "activated": true,
    "langKey": "en",
    "createdBy": "system",
    "createdDate": "2017-08-15T11:47:06.180Z",
    "lastModifiedBy": "system",
    "lastModifiedDate": null,
    "authorities": [
      "ROLE_USER",
      "ROLE_ADMIN"
    ]
  },
  {
    "id": 4,
    "login": "user",
    "firstName": "User",
    "lastName": "User",
    "email": "user@localhost",
    "imageUrl": "",
    "activated": true,
    "langKey": "en",
    "createdBy": "system",
    "createdDate": "2017-08-15T11:47:06.180Z",
    "lastModifiedBy": "system",
    "lastModifiedDate": null,
    "authorities": [
      "ROLE_USER"
    ]
  }
]

We also can choose the German language, since we have chosen English AND German:

Step 3: Create and Download Your Model

You can create and download the model from JDL Studio and access it from your application. If you have followed the instructions above, you need to make sure the file is visible from the JHipster container. For that, you need to place it to the folder you have mapped in step 1 by using the -v docker run flag.

It might be more convenient to cut&paste the content into a file /localdir/blog/model.jh within the container:

entity Blog {
name String required minlength(3),
handle String required minlength(2)
}

entity Entry {
title String required,
content TextBlob required,
date ZonedDateTime required
}

entity Tag {
name String required minlength(2)
}

relationship ManyToOne {
Blog{user(login)} to User,
Entry{blog(name)} to Blog
}

relationship ManyToMany {
Entry{tag(name)} to Tag{entry}
}

paginate Entry, Tag with infinite-scroll

The graphical representation of the model looks like follows:

The modes is re-using the User model that is present in JHipster per default. Now, each blog is mapped to a user. Each blog can have many blog entries and each entry can have many tags, while each tag can be attached to many entries.

In the next step, we will import this model.

Step 4: Import Model

To import the model, we stop the currently running mvnh process (if so), and import the model as follows:

(container)# yo jhispter:import-jdl ./model.jh
Overwrite liquibase/master.xml?: a

Step 5: Start Spring Boot App

After 5 minutes or so, we can start the application again:

(container)# ./mvnw

Note: yarn start alone will not do the trick. See https://stackoverflow.com/questions/43291477/jhipster-cannot-login-after-starting-with-yarn-start-webpack

Step 6: Connect and Login as admin

In a Browser, we need to navigate to localhost:8080 and login as admin with password admin or user with password user. It does not make a difference for now.

We will notice, that now the model entities are available:

Step 7: Create a Blog

Let us choose “Blog” and :

Click Save.

Step 8: Create a Blog Entry

Now let us  on Entities -> Entry:

We can see that the Blog field is not mandatory (otherwise the mouse will show that the Save button cannot be pressed). However, let us choose the Admin’s Blog we have just created:

Note, that I have put only part of the content in headline style.

 

The ID is 2 instead of 1, since I have played around already and I have created and deleted a blog entry already.

Step 9: Add Multi User Support

In this step we will make sure that a user can only edit and delete any entity that belongs to the logged in user.

Step 9.1: Log out and log in as User “user”

We now log out and log in as user “user” with password “user”. You will notice that you can see and edit or delete any entity that belongs to the admin. This is a no-go for any multi-user application and needs to be fixed.

Step 9.2: Add a User’s Blog

Let us first create a User’s Blog and a Blog entity like follows:

We will see both Blog entries:

(since you have not yet fixed the HTML Display, the content will look different in your case, you will see the escaped HTML instead like

However the Date is missing. Oups, when checking with my screenshot above, I have entered a wrong date far in the future. And there is a bad side-effect: when I try to edit or delete the entry, the application does not react.

For the records:

  • we need to make sure that the user input is verified
  • we need to find a way to delete this entry

For now, let us create a new entry:

This is an entry that can be edited and deleted, if needed.

Note: It seems to take quite long, until the entry is loaded. Also, it will be “Loading…” forever, if you click on View of the upper entry and use the “back” button on that page. Maybe this is the case because of the bad entry #2?

If you are reloading the page, it does display, though and the new entry can be edited, if needed:

Step 9.3: Test write access to admin’s entries

Now click  of the admin’s entry and you will see that the user is allowed to edit admin’s entries.

We need to take two measures:

  • the  and  buttons should not be visible (or grayed out and not functional) on entries that do not belong to to the logged in user.
  • the user should not be able to circumvent security by directly calling the Edit or Delete function for entities of other users.
    • calling the URL “http://localhost:8080/#/entry(popup:entry/2/edit)” should return an error
    • but also: corresponding Create/Update/Delete REST calls should return an error.

Step 9.4: Start Application in develop Mode

In order to view the results of any file changes immediately, you need to issue the command

(container)# yarn start

and connect to port 9000 instead of 8080:

Log in as user “user” again.

Step 9.5: Remove foreign Blogs from Blog Table View

For now, as user named “user” we can see both blogs we had created:

A quick&dirty way to remove the  and  buttons from the user’s view is to remove the admin’s blogs from the view, as shown in the JHipster introduction on YouTube:

Let us search for the function getAllBlogs in the java File BlogResource (found on the container as src/main/java/org/jhipster/web/rest/BlogResource.java)

and we change

findAll()

by

findByUserIsCurrentUser()

which is available on JHipster on any model per default.

After restarting ./mvnw, the admin’s blog will be removed from the blog table:

Step 9.6: Restrict Access for Single Blog View

In the previous step, we have removed the admin’s blog from the user’s blog table view. However, the admins’s blog can still be accessed by the user named “user”, if he knows (or guesses) the ID:

This is a topic that is not mentioned in the JHipster introduction on YouTube. So, let that fix too:

On java/org/jhipster/repository/BlogRepository.java, we can reset the return value to “null”, if the found blog does not belong to the logged in user. For that we add:

// file: java/org/jhipster/repository/BlogRepository.java
import import org.jhipster.security.SecurityUtils;
...
    @GetMapping("/blogs/{id}")
    @Timed
    public ResponseEntity getBlog(@PathVariable Long id) {
        log.debug("REST request to get Blog : {}", id);
        Blog blog = blogRepository.findOne(id);
        
        // The user is not allowed to access this blog, if it is not owned by this user:
        if (!blog.getUser().getLogin().equals(SecurityUtils.getCurrentUserLogin())) { blog = null; }

        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(blog));
    }

For that we import the SecurityUtils and compare the user login of the found blog with the user login of the user that is logged in. If it is not equal, we just reset the found value to null.

With that, the blog is not accessible anymore, after we have restarted ./mvnw:

This is, what we want to achieve: blog/3 is not visible because it is not owned by the logged in user.

However, let us double-check, that general access to the blogs is not broken. For that, let us access the user’s blog, /blog/4 in my case:

Yes, that’s it.

Note: since we have not done anything on the DELETE function, any user, who knows how to access the API, will still be able to DELETE or UPDATE the entry. This will be covered in the next blog post, where we will have a closer look to the API created by Spring Boot. To cut is short, following code change (in red) will be shown there:

    @DeleteMapping("/blogs/{id}")
    @Timed
    public ResponseEntity deleteBlog(@PathVariable Long id) {
        log.debug("REST request to delete Blog : {}", id);
        
        Blog blog = blogRepository.findOne(id);     
        
        if(blog != null) {
        	if (!blog.getUser().getLogin().equals(SecurityUtils.getCurrentUserLogin())) { 
        		// The user is not allowed to delete this blog, if it is not owned by this user:
            	        log.debug("Found blog, but user is not allowed to delete it");
            	        return ResponseEntity.status(403).headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
        	} else {
	        	blogRepository.delete(id); 
	        	return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
        	}
        } else {
        	return ResponseEntity.notFound().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
        }
        
        
    }

Note: A similar code change is needed for the PUT (Update) function.

Step 10: Fix the HTML Display

We can see on Entities -> Entry -> View that the HTML display is not correct, since it is escaped:

Let us fix that now. For that, navigate to src/main/webapp/app/entities/entry/entry-detail.component.html (you can do that inside the container via vi, or, since the content is mapped to the Docker host, we also can use an IDE like Visual Studio Code from outside of the container:


There, we can change

by

Unlike what is shown in the JHipster introduction, the application running on localhost:8080 does not seem to recognize, when a file is changed. Even stopping and restarting mvnw did not change anything:

Instead, I have run

(container)# yarn start

within the container and I have connected to the Webpack in development mode on port 9000:

This did the trick.

Now, I can change the files from within the container (by opening an extra session into the Docker container via docker exec -it <containerid> bash, and webpack will re-transpile the code within seconds (but I need to refresh the Browser, it seems).

Note: after running the application some days on my notebook, I have logged in as admin again (without reloading the application) and the change now was visible on port 8080 as well:

I guess, this problem needs some more investigation…

 

Summary

In this blog post, we have created a little blog application using the Java Hipster code generator. For that, we have

  • installed a Docker host, if needed
  • started a Docker JHipster container on the Docker host
  • run and explored the default administration functions of the JHipster application like
    • user management,
    • API exploration (via swagger) and
    • logging
  • edited a model for our application using the JDL Studio Web Page
  • imported the model file of a blog application (blogs, entries, tags) into JHipster
  • explored the new functions of the application
  • tweaked the Spring Boot Read and Delete function, so that only the owner is able to see and delete a blog (this is an addition to what you will find on the JHipster introduction on YouTube.)
  • tweaked the view of the blog within an Angular template to display HTML content correctly

We have seen, how easy it is to import an arbitrary model into the default JHipster application and to create and display the entities defined in the model. In our case, we have created a simple blog application with blogs, entries and tags.

Coming Soon

  • I already have started a Blog Post, where I am exploring the REST API that is automatically generated by JHipster. We will learn, how to use swagger to find the correct curl commands, how to authenticate the service and how to tweak the REST interface, so  only the owner of a blog entity is allowed to delete the entry. Follow this blog, if you are interested in the blog post.
2

Angular 4 Universal: Boosting Performance through Server Side Rendering

This time we will show, how to use server side rendering with Angular 4 (or Angular 2). Like in my previous blog post, we will consume a RESTful Web Service with Angular 4. However, the web page will be displayed almost immediately because of server side rendering, as opposed to the client side rendered situation described in my previous blog post about Angular consuming a REST API. There, we had observed load times of several seconds in situations with low bandwidth between Angular server and REST service.

Why Server Side Rendering?

In our last blog post, Consuming a RESTful Web Service with Angular 4,  we have created an Angular simple single page application that has displayed data from an external resource, the WordPress API. In case of limited bandwidth between client and WordPress API, the latency for some responses were several seconds (!). Because of this fact, the perceived performance of the application was quite poor.

Angular Universal offers a possibility to mitigate the problem: it offers a combination of server side rendering and client side rendering. When the client contacts its server, the complete HTML page is downloaded to the client and the client can display the content immediately. Then the client will perform the REST call and will replace the server-side rendered page by a client-side rendered page. In case of a low performance link to the RESTful interface, this can be perceived by a flickering of the page. This is not perfect, but it is much better than waiting for the page display for seconds.

Now let us begin with our step by step guide.

Phase 1: Run a Server Side Rendered Hello World Page

Step 1.0: Get a Docker Host

This time again, our application will need more than 750 MB RAM. Therefore, we cannot use my beloved Katacoda as our Docker playground, which is limited to this amount of DRAM. Instead, you need to get access to a Docker host with, >~ 2 GB RAM. A nice way of installing an Ubuntu Docker host via Vagrant is described here (search for the term “Install a Docker Host”).

Step 1.1 Run my CentOS Angular Image

On the Docker host, let us start my angular image like follows (the -v $(pwd):/localdir option is only needed, if you want to keep the project folder for later use)

(dockerhost)$ docker run -it -p 8000:8000 -v $(pwd):/localdir oveits/angular_hello_world:centos bash

Then, on the container, we perform following commands:

(container)# git clone https://github.com/FrozenPandaz/ng-universal-demo
(container)# cd ng-universal-demo
(container)# npm i
(container)# npm run watch &
[1] 37

> ng-universal-demo@1.0.0 watch /ng-universal-demo
> webpack --watch


Webpack is watching the files…

Hash: 068db451109474765aa6ca44939d0968e157c122
Version: webpack 2.6.1
Child
    Hash: 068db451109474765aa6
    Time: 15786ms
              Asset       Size  Chunks                    Chunk Names
        0.client.js    2.13 kB       0  [emitted]
          client.js     2.5 MB       1  [emitted]  [big]  main
    0.client.js.map    1.04 kB       0  [emitted]
      client.js.map    3.03 MB       1  [emitted]         main
         index.html  222 bytes          [emitted]
       [0] ./~/rxjs/Observable.js 11.4 kB {1} [built]
       [3] ./~/rxjs/util/root.js 885 bytes {1} [built]
       [7] ./~/@angular/platform-browser/@angular/platform-browser.es5.js 141 kB {1} [built]
       [9] (webpack)/buildin/global.js 509 bytes {1} [built]
      [19] ./~/rxjs/add/operator/map.js 187 bytes {1} [built]
      [39] ./~/@angular/platform-browser-dynamic/@angular/platform-browser-dynamic.es5.js 5.88 kB {1} [built]
      [40] ./src/app/browser-app.module.ts 1.4 kB {1} [built]
      [41] ./~/reflect-metadata/Reflect.js 48 kB {1} [built]
      [42] ./~/zone.js/dist/zone.js 96 kB {1} [built]
      [44] ./~/@angular/compiler/@angular/compiler.es5.js 1.02 MB {1} [built]
      [45] ./src/app/app.module.ts 1.61 kB {1} [built]
      [47] ./src/main.browser.ts 350 bytes {1} [built]
      [49] ./src/modules/transfer-state/browser-transfer-state.module.ts 1.35 kB {1} [built]
      [50] ./~/process/browser.js 5.42 kB {1} [built]
      [78] ./~/rxjs/util/toSubscriber.js 760 bytes {1} [built]
        + 66 hidden modules

    ERROR in /ng-universal-demo/node_modules/rxjs/Subject.d.ts (16,22): Class 'Subject' incorrectly extends base class 'Observable'.
      Types of property 'lift' are incompatible.
        Type '(operator: Operator<T, R>) => Observable' is not assignable to type '(operator: Operator<T, R>) => Observable'.
          Type 'Observable' is not assignable to type 'Observable'.
            Type 'T' is not assignable to type 'R'.
    Child html-webpack-plugin for "index.html":
           [0] ./~/html-webpack-plugin/lib/loader.js!./src/index.html 193 bytes {0} [built]
Child
    Hash: ca44939d0968e157c122
    Time: 21298ms
              Asset     Size  Chunks                    Chunk Names
        0.server.js  2.14 kB       0  [emitted]
          server.js  4.23 MB       1  [emitted]  [big]  main
    0.server.js.map  1.04 kB       0  [emitted]
      server.js.map  5.18 MB       1  [emitted]         main
       [4] ./~/@angular/core/@angular/core.es5.js 489 kB {1} [built]
     [145] ./src/api/app.ts 222 bytes {1} [built]
     [146] ./src/app/server-app.module.ts 2.2 kB {1} [built]
     [147] ./src/routes.ts 80 bytes {1} [built]
     [148] ./~/@nguniversal/express-engine/index.js 196 bytes {1} [built]
     [149] ./~/express/index.js 224 bytes {1} [built]
     [150] ./~/reflect-metadata/Reflect.js 48 kB {1} [built]
     [151] ./~/rxjs/Rx.js 9.65 kB {1} [built]
     [152] ./~/zone.js/dist/zone-node.js 71.1 kB {1} [built]
     [158] ./src/main.server.ts 1.22 kB {1} [built]
     [245] ./~/rxjs/add/operator/bufferCount.js 235 bytes {1} [built]
     [336] ./~/rxjs/add/operator/windowTime.js 229 bytes {1} [built]
     [337] ./~/rxjs/add/operator/windowToggle.js 241 bytes {1} [built]
     [338] ./~/rxjs/add/operator/windowWhen.js 229 bytes {1} [built]
     [339] ./~/rxjs/add/operator/withLatestFrom.js 253 bytes {1} [built]
        + 475 hidden modules

    WARNING in ./~/express/lib/view.js
    80:29-41 Critical dependency: the request of a dependency is an expression

    ERROR in /ng-universal-demo/node_modules/rxjs/Subject.d.ts (16,22): Class 'Subject' incorrectly extends base class 'Observable'.
      Types of property 'lift' are incompatible.
        Type '(operator: Operator<T, R>) => Observable' is not assignable to type '(operator: Operator<T, R>) => Observable'.
          Type 'Observable' is not assignable to type 'Observable'.
            Type 'T' is not assignable to type 'R'.

    ERROR in /ng-universal-demo/node_modules/rxjs/observable/dom/WebSocketSubject.d.ts (24,22): Class 'WebSocketSubject' incorrectly extends base class 'AnonymousSubject'.
      Types of property 'lift' are incompatible.
        Type '(operator: Operator<T, R>) => WebSocketSubject' is not assignable to type '(operator: Operator<T, R>) => Observable'.
          Type 'WebSocketSubject' is not assignable to type 'Observable'.
            Types of property 'operator' are incompatible.
              Type 'Operator<any, R>' is not assignable to type 'Operator<any, T>'.
                Type 'R' is not assignable to type 'T'.

Let us ignore the error in red for now.

(container)# npm run server
[2] 60

> ng-universal-demo@1.0.0 server /ng-universal-demo
> nodemon dist/server.js

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: dist/*server.js src/index.html
[nodemon] starting `node dist/server.js`
Listening at http://localhost:8000
GET: /: 187.390ms
GET: /data: 3.213ms

or if you want to avoid the npm run watch error described as issue on git:angular/angular, you either can fix the typescript version in package.js: "typescript": "2.3.4". Alternatively you can replace the previous two commands by npm run start (the npm run server command, which needs to be restarted often below, is quicker though):

(container)# npm run start
> ng-universal-demo@1.0.0 start /localdir/FrozenPandaz__ng-universal-demo
> npm run build && npm run server


> ng-universal-demo@1.0.0 prebuild /localdir/FrozenPandaz__ng-universal-demo
> npm run clean


> ng-universal-demo@1.0.0 clean /localdir/FrozenPandaz__ng-universal-demo
> rimraf dist


> ng-universal-demo@1.0.0 build /localdir/FrozenPandaz__ng-universal-demo
> webpack

Hash: a746c6e416ab32c2fe97cac872fdf2e493c7e402
Version: webpack 2.6.1
Child
    Hash: a746c6e416ab32c2fe97
    Time: 18765ms
              Asset       Size  Chunks                    Chunk Names
        0.client.js    2.07 kB       0  [emitted]
          client.js     2.5 MB       1  [emitted]  [big]  main
    0.client.js.map    1.07 kB       0  [emitted]
      client.js.map    3.03 MB       1  [emitted]         main
         index.html  222 bytes          [emitted]
       [0] ./~/rxjs/Observable.js 11.4 kB {1} [built]
       [3] ./~/rxjs/util/root.js 885 bytes {1} [built]
       [7] ./~/@angular/platform-browser/@angular/platform-browser.es5.js 141 kB {1} [built]
       [9] (webpack)/buildin/global.js 509 bytes {1} [built]
      [19] ./~/rxjs/add/operator/map.js 187 bytes {1} [built]
      [39] ./~/@angular/platform-browser-dynamic/@angular/platform-browser-dynamic.es5.js 5.88 kB {1} [built]
      [40] ./src/app/browser-app.module.ts 1.35 kB {1} [built]
      [41] ./~/reflect-metadata/Reflect.js 48 kB {1} [built]
      [42] ./~/zone.js/dist/zone.js 96 kB {1} [built]
      [44] ./~/@angular/compiler/@angular/compiler.es5.js 1.02 MB {1} [built]
      [45] ./src/app/app.module.ts 1.55 kB {1} [built]
      [47] ./src/main.browser.ts 350 bytes {1} [built]
      [49] ./src/modules/transfer-state/browser-transfer-state.module.ts 1.31 kB {1} [built]
      [50] ./~/process/browser.js 5.42 kB {1} [built]
      [78] ./~/rxjs/util/toSubscriber.js 760 bytes {1} [built]
        + 66 hidden modules
    Child html-webpack-plugin for "index.html":
           [0] ./~/html-webpack-plugin/lib/loader.js!./src/index.html 193 bytes {0} [built]
Child
    Hash: cac872fdf2e493c7e402
    Time: 24164ms
              Asset     Size  Chunks                    Chunk Names
        0.server.js  2.07 kB       0  [emitted]
          server.js  4.23 MB       1  [emitted]  [big]  main
    0.server.js.map  1.07 kB       0  [emitted]
      server.js.map  5.18 MB       1  [emitted]         main
       [4] ./~/@angular/core/@angular/core.es5.js 489 kB {1} [built]
     [145] ./src/api/app.ts 222 bytes {1} [built]
     [146] ./src/app/server-app.module.ts 2.11 kB {1} [built]
     [147] ./src/routes.ts 80 bytes {1} [built]
     [148] ./~/@nguniversal/express-engine/index.js 196 bytes {1} [built]
     [149] ./~/express/index.js 224 bytes {1} [built]
     [150] ./~/reflect-metadata/Reflect.js 48 kB {1} [built]
     [151] ./~/rxjs/Rx.js 9.65 kB {1} [built]
     [152] ./~/zone.js/dist/zone-node.js 71.1 kB {1} [built]
     [158] ./src/main.server.ts 1.22 kB {1} [built]
     [245] ./~/rxjs/add/operator/bufferCount.js 235 bytes {1} [built]
     [336] ./~/rxjs/add/operator/windowTime.js 229 bytes {1} [built]
     [337] ./~/rxjs/add/operator/windowToggle.js 241 bytes {1} [built]
     [338] ./~/rxjs/add/operator/windowWhen.js 229 bytes {1} [built]
     [339] ./~/rxjs/add/operator/withLatestFrom.js 253 bytes {1} [built]
        + 475 hidden modules

    WARNING in ./~/express/lib/view.js
    80:29-41 Critical dependency: the request of a dependency is an expression

> ng-universal-demo@1.0.0 server /localdir/FrozenPandaz__ng-universal-demo
> nodemon dist/server.js

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: dist/*server.js src/index.html
[nodemon] starting `node dist/server.js`
Listening at http://localhost:8000

Now we connect to port 8000 of the Docker host, I have mapped the internal port 8000 to:

When looking at the source code, we can see the server-side rendered HTML Code:

The text “Universal Demo” and “Hello World” are visible in the source.

This is exactly, what we wanted to see: a web page with the full HTML content, not just the “Loading…” directive that you usually see in Angular Index files.

Phase 2: Create a functional new Link in the main Page

Step 2.1 Create a new Link on the Home Page

With following little change in blue, we will add a new link to the Hello World page:

src/app/app.component.ts

import { Component, OnInit } from '@angular/core'
import { TransferState } from '../modules/transfer-state/transfer-state';

@Component({
  selector: 'demo-app',
  template: `
    <h1>Universal Demo</h1>
    <a routerLink="/">Home</a>
    <a routerLink="/lazy">Lazy</a>
    <a routerLink="/blog">Blog</a>
    <router-outlet></router-outlet>
  `,
  styles: [
    `h1 {
      color: green;
    }`
  ]
})
export class AppComponent implements OnInit {
  constructor(private cache: TransferState) {}
  ngOnInit() {
    this.cache.set('cached', true);
  }
}

From the steps above, npm run watch is still running in the background.

It seems that npm run server needs to be stopped are restarted manually. Since we have started it in the foreground above, a <Ctrl>-C and re-issuing the command is sufficient:

(container)# <Ctrl>-C
(container)# npm run server

After that, the change should be seen immediately in the browser (try pressing F5 to refresh, if this is not the case):

You will notice, though, that the Blog link is not yet functional. When you press F12, choose the “console” tab in the Browser and reload the page, we will see, what is missing:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'blog'

We still need to define the route.

Step 2.2: Create a Route from /blog to a Module

For creating a route, we need to add the /blog route to following file:

src/routes.ts

export const ROUTES: string[] = [
  '/',
  '/lazy',
  '/blog'
];

As we can see in the browser network debugging (F12), the error message does not change:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'blog'

To turn this around, we need a second change: we need to add a link from ‘blog’ to a module. For now, let us point the /blog link to the same module as the /lazy link:

src/app/app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { APP_BASE_HREF, CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeView } from './home/home-view.component';
import { TransferHttpModule } from '../modules/transfer-http/transfer-http.module';


@NgModule({
  imports: [
    CommonModule,
    HttpModule,
    TransferHttpModule,
    RouterModule.forRoot([
      { path: '', component: HomeView, pathMatch: 'full'},
      { path: 'lazy', loadChildren: './+lazy/lazy.module#LazyModule'},
      { path: 'blog', loadChildren: './+lazy/lazy.module#LazyModule'}
    ])
  ],
  declarations: [ AppComponent, HomeView ],
  exports: [ AppComponent ]
})
export class AppModule {}

Now the /blog link is functional and is pointing to the lazy module, showing “i’m lazy”, when clicking on the Blog link.

Now let us create our own module that is pointing to “i’m a blog”

Step 2.3: Create your own Blog Module

Above, we have re-used an existing “LazyModule”. Now, let us create our own module by copying and changing LazyModule:

mkdir src/app/+blog
cp src/app/+lazy/lazy.module.ts src/app/+blog/blog.module.ts

We replace ‘lazy’ by ‘blog’ and ‘Lazy’ by ‘Blog’ in place:

sed -r -i "s/lazy/blog/g" src/app/+blog/blog.module.ts
sed -r -i "s/i'm blog/i'm a blog/g" src/app/+blog/blog.module.ts
sed -r -i "s/Lazy/Blog/g" src/app/+blog/blog.module.ts

After that, the content of blog.module.ts looks like follows:

Now we need to change the route to point to the new BlogModule:

sed -r -i '/blog/s/lazy/blog/g; /blog/s/Lazy/Blog/' src/app/app.module.ts

after that, the file content looks like follows:

src/app/app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { APP_BASE_HREF, CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeView } from './home/home-view.component';
import { TransferHttpModule } from '../modules/transfer-http/transfer-http.module';


@NgModule({
  imports: [
    CommonModule,
    HttpModule,
    TransferHttpModule,
    RouterModule.forRoot([
      { path: '', component: HomeView, pathMatch: 'full'},
      { path: 'lazy', loadChildren: './+lazy/lazy.module#LazyModule'},
      { path: 'blog', loadChildren: './+blog/blog.module#BlogModule'}
    ])
  ],
  declarations: [ AppComponent, HomeView ],
  exports: [ AppComponent ]
})
export class AppModule {}

After restarting the server and reloading the Browser, the link to ‘Blog’ leads to following output:

In the source view, we also can confirm that this is a server-rendered page:

This is exactly, what we wanted to see: a web page with the full HTML content, not just the “Loading…” directive that you usually see in Angular Index files. With that, we can see, that this a server rendered page.

Step 2.4 Add Includes to Browser, Server and Server AOT

To be honest, I am a newbie to Angular and I do not exactly know the function of following three includes. However, I have found them by searching recursively for occurrences of the term “lazy”. Those three includes seem to be needed, although the server side rendering seems to look fine without as well.

This one might be needed for client side rendering within webpack:

src/tsconfig.browser.json

{
  "extends": "../tsconfig.json",
  "angularCompilerOptions": {
    "entryModule": "./app/browser-app.module#BrowserAppModule"
  },
  "include": [
    "./main.browser.ts",
    "./app/+lazy/lazy.module.ts",
    "./app/+blog/blog.module.ts"
  ]
}

AOT stands for “Ahead of Time” and often refers to the compile time. Since we are not compiling, but we are “transpiling” in case of Angular, I guess, the next file will control the server-side pre-tranpiled pages:

src/tsconfig.server.aot.json

{
  "extends": "./tsconfig.server.json",
  "angularCompilerOptions": {
    "genDir": "ngfactory",
    "entryModule": "./app/server-app.module#ServerAppModule"
  },
  "include": [
    "main.server.aot.ts",
    "./app/+lazy/lazy.module.ts",
    "./app/+blog/blog.module.ts",
    "./app/server-app.module.ts"
  ]
}

The next one is a server configuration file for non AOT pages?

src/tsconfig.server.json

{
  "extends": "../tsconfig.json",
  "angularCompilerOptions": {
    "entryModule": "./app/server-app.module#ServerAppModule"
  },
  "include": [
    "main.server.ts",
    "./app/+lazy/lazy.module.ts",
    "./app/+blog/blog.module.ts"
  ]
}

Phase 3: Inserting a WordPress Blog POST via RESTful API

In this phase, we will insert a single WordPress Blog Post via a RESTful API of WordPress, as we had done in my previous blog post “Consuming a RESTful Web Service with Angular 4“. However, this time, the page will be served server-side rendered, which helps for a much better user experience (quicker load time). Especially, the performance is improved substantially, if the WordPress REST API is reachable via a low-bandwidth connection only.

Step 3.1: Change your Blog Module to perform HTTP Requests

In order to perform HTTP requests, we need to adapt the file src/app/+blog/blog.module.ts file, so it performs the same function as did the file src/app/app.component.ts in my previous blog post about Angular consuming HTTP:

  • Like last time, we had to import Http, Response, Headers as well as map as well as Observable. We add Oninit as well, this time.
  • I have replaced the inline template by a templateUrl file. This also helps me to display the content of my Blog Module correctly. However, this will lead to typescript errors as long as the template is not created. We will do this soon.
  • Different from last time, we explicitly have defined private variables title and content. The reason we are not using data.title and data.content in the HTML template is, that the data is null as long as we are waiting for the HTML response and a title and content of null does not exist. When debugging the browser, errors are visible. With the private variable title and content, we do not create such errors.
  • Like last time, we need to define a private variable (_http in our case)with type Http as argument of the constructor
  • Different from last time, we have introduced an OnInit function, which is calling the getMyBlog() function, instead of calling this function in the constructor. However, both possibilities work fine.
  • The getMyBlog() function looks similar to last time. The only difference is, that we set the title and content explicitly.

src/app/+blog/blog.module.ts

import {NgModule, Component, OnInit} from '@angular/core'
import {RouterModule} from '@angular/router'
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/map'
import { Observable } from 'rxjs/Observable';


@Component({
  selector: 'blog-view',
  templateUrl: './blog.module.html'
})

export class BlogView implements OnInit {
  data: any = null;
  title: any = null;
  content: any = null;
  public subs: Observable<string>;

  constructor(private _http: Http) {
  }

  ngOnInit(){
    this.getMyBlog();
  }

  private getMyBlog() {
    return this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/oliverveits.wordpress.com/posts/3078')
                .map((res: Response) => res.json())
                 .subscribe(data => {
                        this.data = data;
                        this.title = this.data.title;
                        this.content = this.data.content;
                        console.log(this.data);
                });
  }

}

@NgModule({
  declarations: [BlogView],
  imports: [
    RouterModule.forChild([
      { path: '', component: BlogView, pathMatch: 'full'}
    ])
  ]
})
export class BlogModule {

}

The function getMyBlog() is retrieving the data of a blog post from the WP.COM Rest API v1.1, similar to what we also have done on the previous blog post Consuming a RESTful Web Service with Angular 4. This time we are writing the title and content in the corresponding public variables, which can be used in the HTML template.

Step 3.2 Create Blog HTML Template

If your npm run watch command is still active, you will notice following error in the console, where the command is running:

    ERROR in ./src/app/+blog/blog.module.ts
    Module not found: Error: Can't resolve './blog.module.html' in '/ng-universal-demo/src/app/+blog'
     @ ./src/app/+blog/blog.module.ts 38:18-47
     @ ./src lazy
     @ ./~/@angular/core/@angular/core.es5.js
     @ ./src/app/browser-app.module.ts
     @ ./src/main.browser.ts

The error will disappear, if we create the following file:

src/app/+blog/blog.module.html

The blog.module.ts file is pointing to a HTML templateUrl on ./blog.module.html with following content (as picture, since WordPress is confused about the HTML code, even if set in <pre>…</pre> tags):

Now the error is disappeared. If we restart the npm run server and the Browser content (e.g. press F5), we will see following content:

And different from last time, we not only see “Loading…” in the source of the page, but we will see the full HTML content:

To be honest, we see the content more often than we need: we see it as “innerHtml”, but we also see it explicitly as data within the tags. Large pages will have doubled size. Okay, seeing it twice is better than seeing it no time at all. Let us call it not perfect, but still …

Summary

In this blog post, we have performed following tasks:

  • Phase 1: Run a Server Side Rendered Hello World Page
    • Here we could show that the server provides the browser with the full HTML content
  • Phase 2: Create a functional new Link in the main Page
    • Those steps are about Link creation and routing to a module
  • Phase 3: Inserting a WordPress Blog POST via RESTful API
    • In those steps we have shown how to create a component that retrieves content from a REST API and how to display the information in a browser

We could see, that the user is presented with the page content much more quickly than was the case in a client rendered solution of my previous blog. Especially, if there is a low bandwidth connection between Angular server and REST service, the user perception is improved a lot by server side rendering: the content is displayed almost immediately as opposed of the several second loading time we had experienced in case of client side rendering.

Appendix: Why using innerHtml?

Let us first demonstrate, what happens, if we use following template: The HTML template is making use of the variables “title” and “content” we have defined in the blog.module.ts before. We need to make use of this innerHtml trick in order to display the HTML-based content correctly. If we would use “{{content}}” instead, we will see escaped HTML.

This is not, what we want. If we change the template by following content:

or better (see this StackOverflow Q&A that states “Sanitized content can’t be bound using prop="{{sanitizedContent}}" because {{}} stringyfies the value before it is assigned which breaks sanitization.”; we do not yet sanitize here, but we might do so in the future):

With this, we will get, what we want:

Appendix: Webpack Problem

Error

Webpack has been initialised using a configuration object that does not match the API schema.

How to reproduce

docker run -it -p 8081:8000 -v $(pwd):/localdir oveits/angular_hello_world:centos bash
cd /localdir
git clone https://github.com/robwormald/ng-universal-demo
cd ng-universal-demo
npm i
$ npm start
> ng-universal-demo@1.0.0 start /localdir/ng-universal-demo
> npm run build && npm run server


> ng-universal-demo@1.0.0 build /localdir/ng-universal-demo
> webpack -p

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.output.path: The provided value "dist" is not an absolute path!

Workaround

Clone the fork https://github.com/FrozenPandaz/ng-universal-demo instead.

Further Reading

0

Vagrant on CentOS 7 – Setting up Test Environments the easy Way

After stumbling upon several guides still describing a Vagrant installation via a RubyGem – which is no longer supported – the following article was created and will provide a quick setup guide on how to setup Vagrant on CentOS 7. All commands used in this guide are executed having root permissions.

Setting up VirtualBox as your Vagrant Provider

Since Vagrant is a utility to manage the lifecycle of virtual machines but doesn’t provide them it relies on providers. As described here Vagrant supports different providers by default such as  VirtualBoxHyper-V, and Docker. Due to the wide availability of VirtualBox it will be used in this guide.

In order to install VirtualBox we first need to add the VirtualBox repository:

cd /etc/yum.repos.d
yum install -y wget
wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo

Because the installation of VirtualBox will require building kernel modules DKMS – Dynamic Kernel Module Support – from the EPEL repository will be installed first:

yum -y install epel-release
yum -y install dkms

In order to find the latest available VirtualBox version execute:

yum provides VirtualBox

and install via:

yum -y install VirtualBox-5.1-5.1.22_115126_el7-1.x86_64

Installing Vagrant

The latest Vagrant packages are available here. With a simple:

yum install https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.rpm

the installation is completed in seconds.

Setting up your project environment

Vagrant projects are managed via Vagrantfiles. In order to start a new project create a dedicated folder and execute Vagrant init which will automatically create a new Vagrantfile:

mkdir Test_Project
cd Test_Project/
vagrant init

Now that we have our environment set up it is time to add a new base image. A variety of images – also called boxes – are available at https://app.vagrantup.com. We will add the latest Ubuntu Image to our boxes with:

vagrant box add ubuntu/xenial64

After the box was added locally we need to change the Vagrantfile which got created before. By changing the parameter config.vm.box from base to ubuntu/xenial64 Vagrant is configured to run the box we added previously.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
end

Starting your  machine

Now that everything is set up the new machine is started with:

vagrant up

and is accessible by ssh executing:

vagrant ssh

The machine can be stopped with:

vagrant halt

Note: If you can not connect via ssh because you are asked for a password it might be related to: https://bugs.launchpad.net/cloud-images/+bug/1569237. You can switch to ubuntu/trusty64 by deleting your current Vagrantfile:

rm Vagrantfile

adding the ubuntu/trusty64 box:

vagrant box add ubuntu/trusty64

and create a new Vagrantfile containing a proper config.vm.box parameter:

vagrant init ubuntu/trusty64
3

Consuming a REST Service with Angular 5: a Step-by-step Guide

In this step-by-step tutorial, we will learn how to consume a REST Service with Angular 5.2. We will perform following steps:

  • install Angular using the Angular CLI
  • create an Angular single page application that is consuming a RESTful Web Service with Angular
  • make sure that the HTML content is trusted and fully functional.

Updated on 2018-03-24:

  • tested with Angular 5.2
  • added: how to tell Angular to trust HTML content retrieved from an API

Updated on 2018-04-06:

  • Updated from deprecated HttpModule (@angular/http) to new HttpClientModule (@angular/common/http)

Consuming a REST Service with Angular

At the end of this tutorial, we will have created a simple Angular page that is displaying the contents of a previous blog post on WordPress. The content has been retrieved from the WordPress REST API in JSON format. The title and content will be displayed:

The rest of the article is organized into two phases:

  1. Installing Angular CLI on CentOS
  2. Programming the Angular REST Client

Let us start:

Phase 1: Install Angular CLI on CentOS

Step 1.0: Install a Docker Host

This time, we will need to use a real Docker host: i.e., different from our last Angular Hello World Quickstart post, this time it is not possible to use Katacoda as our Docker Host playground. We will install Angular CLI, and this requires more Memory than is provided by Katacoda. If you have no access to a Docker host yet and if you are working on a Windows machine, you might want to follow the instructions found here; search for the term “Install a Docker Host”. It describes step by step how to install a Docker host on Virtualbox using Vagrant. I have made good experiences with that approach. For other operating systems, you may want to consult the official Docker installation instructions.

Note: the official Docker installation instructions for Windows might work as well, but years ago, when I had started with Docker, the official solution had urged me to write a blog post “Why is it so hard to install Docker on Windows”). Those were the times with boot2docker. However, the situation has improved from what I have heard from other colleagues that have started with Docker more recently. Still, I like the Vagrant approach a lot, and I will stick to that solution.

Step 1.1: Start CentOS Container

Let us create a Docker CentOS container on a Docker host:

docker run -it -p 4200:4200 centos bash

We have mapped TCP container port 4200 to the Docker host port 4200 since we want to access the service on this port from outside.

Persistent alternative (optional): to persist the Angular project we will be creating on the Docker host, it makes sense to map a  volume from Docker host to Docker container. I am using Vagrant with a synced folder on /vagrant (from point of view of the VirtualBox VM), so I have created a folder on this place for this purpose:

mkdir /vagrant/angular-5-on-centos; cd /vagrant/angular-5-on-centos
docker run -it -p 4200:4200 -v $(pwd):/localdir centos bash

Step 1.2: Install NodeJS and Angular CLI

on the container:

(container)# yum install -y epel-release
(container)# yum install -y https://kojipkgs.fedoraproject.org//packages/http-parser/2.7.1/3.el7/x86_64/http-parser-2.7.1-3.el7.x86_64.rpm
(container)# yum install -y nodejs
(container)# npm install -g @angular/cli
(container)# yum install -y git

Note: the second command has been introduced recently, since the latest epel-release is missing the http parser library. See SvennD’s blog for more details.

Note: the git install command has been introduced to allow the creation of a .gitignore file in the next step.

In order to verify the installation, let us check the version of Angular CLI and node:

(container)# ng -v
[root@3cb1edd13cf2 /]# ng -v

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.7.3
Node: 6.12.3
OS: linux x64
Angular:
...

Step 1.3: Create new Project

Step 1.3.1: Create the Project

Now let us create a project:

cd /localdir
ng new my-project-name
cd my-project-name

Depending on the speed of your Internet connection, the second command might take some minutes.

We have not installed GIT yet, so we will get following error message before the at the end:

/bin/sh: git: command not found
Project ‘my-project-name’ successfully created.

Step 1.3.2 Check the Version of the Project

 grep @angular/core package.json
 "@angular/core": "^5.2.0",

We can see that angular has generated a v5.2 Angular project in this case.

Step 1.4: Start the Service

ng serve --host 0.0.0.0
** NG Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **
Date: 2018-03-24T14:24:53.729Z
Hash: 5aab41746ecad14f6d3b
Time: 17472ms
chunk {inline} inline.bundle.js (inline) 3.85 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 17.9 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 549 kB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 41.5 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 7.42 MB [initial] [rendered]

webpack: Compiled successfully.

Step 1.5: Connect to the Service

If you are running the docker host locally, or if your local port 4200 is mapped to your docker host’s port 4200, we get:

Phase 2: Programming the Angular REST API Client

In this phase, we will see, how easy it is in Angular 2 to 5 to retrieve and display data from a REST API.

Step 2.1: Add HTTP Module

First, we need to tell Angular that we will use the HttpModule. For that, we edit src/app/app.module.ts (added parts in blue)

import { BrowserModule } from '@angular/platform-browser';
import { HttpClient } from '@angular/common/http';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 2.2: Configure Component to use HTTP

In a second step, we configure Edit src/app/app.component.ts:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  public title : String = 'Loading title...';
  public content : String = 'Loading content...';

  constructor(private _http: HttpClient) {
  }

  ngOnInit() {
     this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/vocon-it.com/posts/3078')
                 .subscribe(data => {
                        this.title = data.title;
                        this.content = data.content;
                        console.log(data);
                });
  }
}

Step 2.3: Adapt HTML Template

Remove all content of src/app/app.component.html and replace it with the following content:

<h2 style="color:blue;">The following content has been retrieved from the WordPress REST API:</h2>
<h1 style="font-size: 250%;" [innerHTML]="title">Loading Title...</h1>
<div [innerHTML]="content">Loading Content...</div>

(Sometimes WordPress seemed to have a problem displaying the content correctly, so I post it as a screenshot in addition to the text that might disappear when trying to save the page)

After that, we will see that the browser is displaying following content:

Step 2.4: Fix the Table of Contents

My WordPress page will return a table of contents (TOC) because I have installed TOC plugin. However, you will note that the links to the headlines do not work. We will see the reason in the debug console (press F12) of the Chrome browser:

WARNING: sanitizing HTML stripped some content (see http://g.co/ng/security#xss).

The problem and the workaround is described in my blog post “Angular 4: Automatic Table of Contents – Part 2: Adding Links“.

The problem arises from the fact that Angular does not trust dynamically assigned innerHTML per default. It will remove any IDs found in the content. We can see this easily in the in the elements section of the Chrome browser after entering debug mode (F12):

Angular has removed the ID of the span element. Hence, the links to those IDs will not work.

To fix this problem, we have to tell Angular explicitly, that it should trust the innerHTML content. This is done in the component as follows (changes in blue):

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  public title : SafeHtml|String = 'Loading title...';
  public content : SafeHtml|String = 'Loading content...';

  constructor(private _http: HttpClient, private _sanitizer: DomSanitizer) {
  }

  ngOnInit() {
     this._http.get('https://public-api.wordpress.com/rest/v1.1/sites/vocon-it.com/posts/3078')
                 .subscribe(data => {
                        this.title = this._sanitizer.bypassSecurityTrustHtml(data.title);
                        this.content = this._sanitizer.bypassSecurityTrustHtml(data.content);
                        console.log(data);
                });
  }
}

We check again the element in the debug mode of the chrome browser (F12): the IDs are present now:

With that, the links work as expected: When clicking the table of contents link, the browser will jump to the corresponding headline:

Summary

In this hello world style step-by-step guide, we have learned how to

  • install an Angular 5 project using the Angular CLI and
  • consume the WordPress API for retrieving and displaying a blog post title and HTML content.
  • tell Angular to trust the “innerHTML” content retrieved from the API

Next steps

  • Add HTTP Headers (e.g. needed for accessing authenticated REST services).
  • Separate the REST data function into its own service as shown in this article.
  • Retrieve a list of Blog Posts instead of a single post only.
    • Figure out to handle the fact that retrieving all posts takes a lot of time initially; the WordPress REST API does not seem to allow to retrieve the list of titles only. Do we need to create an improved REST service that is quicker and allows to

Appendix: Exploring the WordPress REST API

The WordPress API can be explored via the WordPress.com REST API console.

You need to login via  and your WordPress.com credentials.

There seem to be several types and versions of APIs:

  • WP.COM API
    • v1.1 (default)
    • v1
  • WP REST API
    • wp/v2
    • wpcom/v2

In this Proof of Concept / Hello World, we are using the WP.COM API V1.1 to retrieve the content of a single WordPress blog post of mine:

https://public-api.wordpress.com/rest/v1.1/sites/vocon-it.com/posts/3078

However, we can also use the WP REST API wp/v2:

https://public-api.wordpress.com/wp/v2/sites/oliverveits.wordpress.com/posts/3078

Because of the organization of the wp/v2 result, we need to exchange data.title by data.title.rendered and data.content by data.content.rendered in the HTML template, though.

Note: wp/v2 API does not seem to work with our new primary domain vocon-it.com. We have changed the primary stite from oliverveits.wordpress.com to vocon-it.com a week ago.

References: