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
For running the client locally, you'll need either an API key or a JWT auth token:
You can generate a development-only JWT token using Google Cloud CLI:
$ gcloud auth print-identity-token
See get-id-token#generic-dev for more info.
Then run the client using this token:
$ node client.js -h localhost:50051 -j YOUR_JWT_TOKEN
Alternatively, you can use an API key from your Google Cloud project:
$ node client.js -h localhost:50051 -k YOUR_API_KEY
You can create API keys in the Google Cloud Console.
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.*.yaml
file 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 api.pb
-
In
api_config.yaml
, replaceMY_PROJECT_ID
andSERVICE-ACCOUNT-ID
with your Project ID and your service account's email address respectively. -
Deploy your service's configuration to Endpoints. Take note of your service's name once the deployment completes.
$ gcloud endpoints services deploy api.pb api_config.yaml
...
- Build a Docker image for later use using the following command. Make sure to replace
[YOUR_PROJECT_ID]
with your Project ID.
$ gcloud builds submit --tag gcr.io/[YOUR_PROJECT_ID]/endpoints-example:1.0 .
...
Service Configuration [SERVICE_CONFIG_ID] uploaded for service [SERVICE_NAME]
-
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_GOOGLE_CLOUD_PROJECT]
and[YOUR_SERVICE_NAME]
with your GCloud Project ID and your service's name respectively.
$ sudo docker run --detach --name=helloworld gcr.io/[YOUR_GOOGLE_CLOUD_PROJECT]/endpoints-example:1.0
$ sudo docker run \
--detach \
--name=esp \
--publish=80:9000 \
--link=helloworld:helloworld \
gcr.io/endpoints-release/endpoints-runtime:1 \
--service=[YOUR_SERVICE_NAME] \
--rollout_strategy=managed \
--http2_port=9000 \
--backend=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
kubectl
to 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.yaml
file, and replaceGOOGLE_CLOUD_PROJECT
andSERVICE_NAME
with your GCloud Project ID and your service's name. -
Add a Kubernetes service to the cluster you created. Note that Kubernetes services should not be confused with Endpoints services.
$ kubectl create -f deployment.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]
In order to enable HTTP/JSON transcoding, use the protos/http_helloworld.proto
definition and the http_deployment.yaml
Kubernetes deployment.
- In order to compile the gRPC definition with HTTP annotations, you need a copy of the googleapis proto definitions.
$ GOOGLEAPIS=...
$ git clone https://github.com/googleapis/googleapis $GOOGLEAPIS
- Compile the gRPC definition with HTTP annotations:
$ protoc \
--proto_path=protos \
--proto_path=$GOOGLEAPIS \
--include_imports \
--include_source_info \
--descriptor_set_out api.pb \
helloworld.proto
- Deploy the API definition with HTTP annotations:
$ gcloud endpoints services deploy api.pb api_config.yaml
- Deploy the Endpoints Proxy (ESP) with the HTTP 1.1 port enabled.
Make sure to update the content of
http_deployment.yaml
and replace the placeholder[SERVICE_NAME]
and[GOOGLE_CLOUD_PROJECT]
.
$ kubectl apply -f http_deployment.yaml
- Test the HTTP 1.1 Transcoding interface
$ curl "http://[SERVICE_IP_ADDRESS]/v1/sayHello?key=${API_KEY}&name=World"
{"message":"Hello World"}
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.