This sample demonstrates how to use Google Cloud Endpoints with Node.js.
For a complete walkthrough showing how to run this sample in different environments, see the Google Cloud Endpoints Quickstarts.
$ node server.js -p 50051
$ node client.js -h localhost:50051
Make sure you have gcloud and Node.js installed.
To update gcloud, use the gcloud components update command.
- Determine the appropriate API configuration file to use based on your authentication method.
- JSON Web Tokens: use
api_config.jwt.yaml - API keys: use
api_config.key.yaml
- Rename the
api_config.*.yamlfile you chose in Step 1 toapi_config.yaml.
-
Install protoc.
-
Compile the proto file using protoc.
$ protoc --include_imports --include_source_info protos/helloworld.proto --descriptor_set_out out.pb
-
In
api_config.yaml, replaceMY_PROJECT_IDandSERVICE-ACCOUNT-IDwith your Project ID and your service account's email address respectively. -
Deploy your service's configuration to Endpoints. Take note of your service's config ID and name once the deployment completes.
$ gcloud service-management deploy out.pb api_config.yaml
...
Service Configuration [SERVICE_CONFIG_ID] uploaded for service [SERVICE_NAME]
- Build a Docker image for later use using the following command. Make sure to replace
[YOUR_PROJECT_ID]with your Project ID.
$ gcloud container builds submit --tag gcr.io/[YOUR_PROJECT_ID]/endpoints-example:1.0 .
-
Create a Compute Engine instance. Be sure to check Allow HTTP traffic and Allow HTTPS traffic when creating the instance.
-
Once your instance is created, take note of its IP address.
Note: this IP address is ephemeral by default, and may change unexpectedly. If you plan to use this instance in the future, reserve a static IP address instead.
- SSH into your instance, and install Docker.
$ sudo apt-get update
$ sudo apt-get install docker.io
- Using the SSH connection to your instance, initialize the required Docker images in the order specified below. Replace
[YOUR_GCLOUD_PROJECT],[YOUR_SERVICE_NAME]and[YOUR_SERVICE_CONFIG_ID]with your GCloud Project ID, your service's name and your service's config ID respectively.
$ sudo docker run -d --name=helloworld gcr.io/[YOUR_GCLOUD_PROJECT]/endpoints-example:1.0
$ sudo docker run --detach --name=esp \
-p 80:9000 \
--link=helloworld:helloworld \
gcr.io/endpoints-release/endpoints-runtime:1 \
-s [YOUR_SERVICE_NAME] \
-v [YOUR_SERVICE_CONFIG_ID] \
-P 9000 \
-a grpc://helloworld:50051
- On your local machine, use the client to test your Endpoints deployment. Replace
[YOUR_INSTANCE_IP_ADDRESS]with your instance's external IP address, and[YOUR_API_KEY]with a valid Google Cloud Platform API key.
$ node client.js -h [YOUR_INSTANCE_IP_ADDRESS]:80 -k [YOUR_API_KEY]
- If you haven't already, install
kubectl.
$ gcloud components install kubectl
-
Create a container cluster with the default settings. Remember the cluster's name and zone, as you will need these later.
-
Configure
kubectlto have access to the cluster. Replace[YOUR_CLUSTER_NAME]and[YOUR_CLUSTER_ZONE]with your cluster's name and zone respectively.
$ gcloud container clusters get-credentials [YOUR_CLUSTER_NAME] --zone [YOUR_CLUSTER_ZONE]
-
Edit the
container_engine.yamlfile, and replaceGCLOUD_PROJECT,SERVICE_NAME, andSERVICE_CONFIGwith your Project ID and your Endpoints service's name and config ID respectively. -
Add a Kubernetes service to the cluster you created. Note that Kubernetes services should not be confused with Endpoints services.
$ kubectl create -f container-engine.yaml
- Get the external IP of your service. This may take a few minutes to be provisioned.
$ kubectl get service
You can use the included client to test your Endpoints deployment.
- Determine your service's IP address.
-
If your service is hosted on Compute Engine, this will be your instance's external IP address.
-
If your service is hosted on Container Engine, this will be your service's external IP address.
- Run the client to connect to your service. When running the following commands, replace
[YOUR_IP_ADDRESS]with the IP address you found in Step 1.
-
If you're using an API key, run the following command and replace
[YOUR_API_KEY]with the appropriate API key.$ node client.js -h [YOUR_CLUSTER_IP_ADDRESS]:80 -k [YOUR_API_KEY] -
If you're using a JSON Web Token, run the following command and replace
[YOUR_JWT_AUTHTOKEN]with a valid JSON Web Token.$ node client.js -h [YOUR_CLUSTER_IP_ADDRESS]:80 -j [YOUR_JWT_AUTHTOKEN]
If you do not intend to use the resources you created for this tutorial in the future, delete your VM instances and/or container clusters to prevent additional charges.
If you're having issues with this tutorial, here are some things to try:
- Check your GCE/GKE instance's logs
- Make sure your Compute Engine instance's firewall permits TCP access to port 80
If those suggestions don't solve your problem, please let us know or submit a pull request.