Skip to content

Commit 814d330

Browse files
committed
Added 14 Google App Engine samples.
1 parent b2272ca commit 814d330

File tree

164 files changed

+2416
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+2416
-216
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015, Google, Inc.
1+
# Copyright 2015-2016, Google, Inc.
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at

README.md

+94-27
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
# Google Cloud Platform NodeJS Samples
22

3-
This repository holds the samples used in the nodejs documentation on
4-
[cloud.google.com/nodejs](https://cloud.google.com/nodejs).
3+
This repository holds Node.js samples used throughout [cloud.google.com]().
54

65
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/nodejs-docs-samples.svg)](https://travis-ci.org/GoogleCloudPlatform/nodejs-docs-samples)
76

7+
## Table of Contents
8+
9+
* [Google App Engine](#google-app-engine)
10+
* [Google Cloud Logging](#google-cloud-logging)
11+
* [Google Cloud Storage](#google-cloud-storage)
12+
* [Google Prediction API](#google-prediction-api)
13+
* [Other Example Apps](#other-example-apps)
14+
* [More Information](#more-information)
15+
* [Contributing](#contributing)
16+
* [License](#license)
17+
818
## Google App Engine
919

1020
This is a collection of samples and instructions to run common nodejs frameworks
1121
and applications on [Google App Engine](http://cloud.google.com/nodejs).
1222

13-
### Frameworks
23+
__Google (Cloud) Products__
24+
25+
- Google Analytics - [Source code][aeanalytics_1]
26+
- Google Cloud Logging - [Source code][aelogging_1]
27+
- Google Cloud Pub/Sub - [Source code][aepubsub_1]
28+
- Google Cloud Storage - [Source code][aestorage_1]
29+
30+
__Frameworks__
1431

1532
- Express.js - [Source code][express_1] | [App Engine Tutorial][express_2] | [Live demo][express_3] | [Documentation][express_4]
1633
- Express.js + Memcached Sessions - [Source code][express_5] | [Documentation][express_6]
@@ -22,25 +39,51 @@ and applications on [Google App Engine](http://cloud.google.com/nodejs).
2239
- Restify.js - [Source code][restify_1] | [App Engine Tutorial][restify_2] | [Live demo][restify_3] | [Documentation][restify_4]
2340
- Sails.js - [Source code][sails_1] | [App Engine Tutorial][sails_2] | [Live demo][sails_3] | [Documentation][sails_4]
2441

25-
### Databases
42+
__Databases__
2643

44+
- Google Cloud Datastore - [Source code][aedatastore_1]
45+
- Google Cloud SQL - [Source code][aecloudsql_1]
46+
- Memcached - [Source code][memcached_1]
2747
- MongoDB - [Source code][mongodb_1] | [App Engine Tutorial][mongodb_2] | [Documentation][mongodb_3]
2848
- Redis - [Source code][redis_1] | [App Engine Tutorial][redis_2] | [Documentation][redis_3]
2949

30-
### Tools
50+
__Tools__
3151

3252
- gcloud-node - [Source code][gcloud_1] | [Documentation][gcloud_2]
3353
- Bower - [Source code][bower_1] | [App Engine Tutorial][bower_2] | [Documentation][bower_3]
3454
- Grunt - [Source code][grunt_1] | [App Engine Tutorial][grunt_2] | [Live demo][grunt_3] | [Documentation][grunt_4]
3555
- Mailgun - [Source code][mailgun_1] | [App Engine Tutorial][mailgun_2] | [Documentation][mailgun_3]
3656
- Sendgrid - [Source code][sendgrid_1] | [App Engine Tutorial][sendgrid_2] | [Documentation][sendgrid_3]
57+
- Twilio - [Source code][twilio_1]
3758
- Webpack - [Source code][webpack_1] | [App Engine Tutorial][webpack_2] | [Documentation][webpack_3]
59+
- WebSockets - [Source code][websockets_1]
60+
61+
__Other Examples__
62+
63+
- Express.js Hello World - [Source code][expresshw_1]
64+
- Extending the runtime - [Source code][aeextending_1]
65+
- Reading/writing from/to disk - [Source code][aedisk_1]
66+
- Serving static files - [Source code][aestaticfiles_1]
67+
68+
## Google Cloud Datastore
69+
70+
- Tasks sample - [Source code][datastore_1] | [Documentation][datastore_2]
71+
72+
## Google Cloud Logging
73+
74+
- Reading logs sample - [Source code][logging_read_1] | [Documentation][logging_read_2]
75+
- Writing logs sample - [Source code][logging_write_1] | [Documentation][logging_write_2]
76+
- Exporting logs sample - [Source code][logging_export_1] | [Documentation][logging_export_2]
77+
78+
## Google Cloud Storage
79+
80+
- Auth sample - [Source code][storage_1] | [Documentation][storage_2]
3881

39-
## Google Storage
82+
## Google Prediction API
4083

41-
- Auth sample - [Source code][storage_1] | [Google Cloud Docs][storage_2]
84+
- Hosted Models sample - [Source code][predictionapi_1] | [Documentation][predictionapi_2]
4285

43-
## Example Apps
86+
## Other Example Apps
4487

4588
- nodejs-getting-started - [Source code][nodejs_1] | [App Engine Tutorial 1][nodejs_2] | [App Engine Tutorial 2][nodejs_3]
4689
- gcloud-node-todos - [Source code][todos_1]
@@ -54,7 +97,7 @@ and applications on [Google App Engine](http://cloud.google.com/nodejs).
5497
- [Using the `gcloud` npm module](https://googlecloudplatform.github.io/gcloud-node/#/)
5598
- [Logging to Google Cloud with Winston](https://github.com/GoogleCloudPlatform/winston-gae)
5699

57-
## Contributing changes
100+
## Contributing
58101

59102
Contributions welcome!
60103

@@ -68,6 +111,9 @@ See [CONTRIBUTING.md](https://github.com/GoogleCloudPlatform/nodejs-docs-samples
68111
1. Start Redis
69112
1. Start Memcached
70113
1. Set the `TEST_PROJECT_ID` environment variable to id of your project
114+
1. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path to
115+
a service account file. You can download one from your Google project's
116+
"permissions" page.
71117
1. `npm test`
72118

73119
Since the tests use [Mocha.js](https://mochajs.org/), you can use the `--grep`
@@ -77,42 +123,34 @@ option causes the matched tests to be excluded instead of included.
77123
__Run only the tests that match a pattern:__
78124

79125

80-
```
81-
npm test -- -- --grep <pattern>
82-
```
126+
npm test -- -- --grep <pattern>
83127

84128
__Only run the tests for the `datastore` sample:__
85129

86-
```
87-
npm test -- -- --grep datastore
88-
```
130+
npm test -- -- --grep datastore
89131

90132
__Skip the tests that match a pattern:__
91133

92-
```
93-
npm test -- -- --grep <pattern> --invert
94-
```
134+
npm test -- -- --grep <pattern> --invert
95135

96136
__Run all but the `datastore` tests:__
97137

98-
```
99-
npm test -- -- --grep datastore --invert
100-
```
138+
npm test -- -- --grep datastore --invert
101139

102140
__Skip the tests that require Redis and Memcached:__
103141

104-
```
105-
npm test -- -- --grep "express-memcached-session|redis" --invert
106-
```
142+
npm test -- -- --grep "express-memcached-session|redis" --invert
107143

108-
## Licensing
144+
## License
109145

110146
Apache Version 2.0
111147

112148
See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/LICENSE)
113149

114-
[storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js
115-
[storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples
150+
[aeanalytics_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/analytics
151+
[aelogging_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/logging
152+
[aepubsub_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/pubsub
153+
[aestorage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/storage
116154

117155
[express_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/express
118156
[express_2]: https://cloud.google.com/nodejs/resources/frameworks/express
@@ -156,6 +194,10 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
156194
[sails_3]: http://sails-dot-nodejs-docs-samples.appspot.com
157195
[sails_4]: http://sailsjs.org/
158196

197+
[aedatastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/datastore
198+
[aecloudsql_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/cloudsql
199+
[memcached_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/memcached
200+
159201
[mongodb_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/mongodb
160202
[mongodb_2]: https://cloud.google.com/nodejs/resources/databases/mongo
161203
[mongodb_3]: https://docs.mongodb.org/
@@ -184,10 +226,35 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
184226
[sendgrid_2]: https://cloud.google.com/nodejs/resources/tools/sendgrid
185227
[sendgrid_3]: http://sendgrid.com/
186228

229+
[twilio_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/twilio
230+
187231
[webpack_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/webpack
188232
[webpack_2]: https://cloud.google.com/nodejs/resources/tools/webpack
189233
[webpack_3]: https://webpack.github.io/
190234

235+
[websockets_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/websockets
236+
237+
[expresshw_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/hello-world
238+
[aedisk_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/disk
239+
[aeextending_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/extending-runtime
240+
[aestaticfiles_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/static-files
241+
242+
[datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js
243+
[datastore_2]: https://cloud-dot-devsite.googleplex.com/datastore/docs/concepts/overview
244+
245+
[logging_read_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/list.js
246+
[logging_read_2]: https://cloud-dot-devsite.googleplex.com/logging/docs/api/tasks/authorization
247+
[logging_write_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/write.js
248+
[logging_write_2]: https://cloud.google.com/logging/docs/api/tasks/creating-logs
249+
[logging_export_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/export.js
250+
[logging_export_2]: https://cloud.google.com/logging/docs/api/tasks/exporting-logs
251+
252+
[storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js
253+
[storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples
254+
255+
[predictionapi_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/prediction/hostedmodels.js
256+
[predictionapi_2]: https://cloud.google.com/prediction/docs/developer-guide#predictionfromappengine
257+
191258
[nodejs_1]: https://github.com/GoogleCloudPlatform/nodejs-getting-started
192259
[nodejs_2]: https://cloud.google.com/nodejs/getting-started/hello-world
193260
[nodejs_3]: https://cloud.google.com/nodejs/getting-started/tutorial-app

appengine/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Google App Engine Node.js Samples
2+
3+
These are samples for using Node.js on Google App Engine Managed VMs. These
4+
samples are referenced from the [docs](https://cloud.google.com/appengine/docs).
5+
6+
See our other [Google Cloud Platform github repos](https://github.com/GoogleCloudPlatform)
7+
for sample applications and scaffolding for other frameworks and use cases.
8+
9+
## Run Locally
10+
11+
Some samples have specific instructions. If there is a README in the sample
12+
folder, please refer to it for any additional steps required to run the sample.
13+
14+
In general, the samples typically require:
15+
16+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the
17+
[gcloud tool](https://cloud.google.com/sdk/gcloud/), and
18+
[gcloud app component](https://cloud.google.com/sdk/gcloud-app).
19+
1. Setup the gcloud tool. This provides authentication to Google Cloud APIs and
20+
services.
21+
22+
gcloud init
23+
24+
1. Clone this repo.
25+
26+
git clone https://github.com/GoogleCloudPlatform/<REPO NAME>.git
27+
28+
1. Open a sample folder, install dependencies, and run the sample:
29+
30+
cd <sample-folder>/
31+
npm install
32+
npm start
33+
34+
1. Visit the application at [http://localhost:8080](http://localhost:8080).
35+
36+
## Deploying
37+
38+
Some samples in this repositories may have special deployment instructions.
39+
Refer to the README file in the sample folder.
40+
41+
1. Use the [Google Developers Console](https://console.developer.google.com) to
42+
create a project/app id. (App id and project id are identical.)
43+
1. Setup the gcloud tool, if you haven't already.
44+
45+
gcloud init
46+
47+
1. Use gcloud to deploy your app.
48+
49+
gcloud preview app deploy
50+
51+
1. Awesome! Your application is now live at `your-app-id.appspot.com`.

appengine/analytics/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Google Analytics Measurement Protocol on Google App Engine
2+
3+
This sample demonstrates how to use the [Google Analytics Measurement Protocol](https://developers.google.com/analytics/devguides/collection/protocol/v1/)
4+
(or any other SQL server) on [Google App Engine Managed VMs](https://cloud.google.com/appengine).
5+
6+
## Setup
7+
8+
Before you can run or deploy the sample, you need to do the following:
9+
10+
1. Create a Google Analytics Property and obtain the Tracking ID.
11+
1. Update the environment variables in in `app.yaml` with your Tracking ID.
12+
13+
## Running locally
14+
15+
Refer to the [appengine/README.md](../README.md) file for instructions on
16+
running and deploying.
17+
18+
To run locally, set the environment variables via your shell before running the
19+
sample:
20+
21+
export GA_TRACKING_ID=<your-tracking-id>
22+
npm install
23+
npm start

appengine/analytics/app.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START app]
16+
'use strict';
17+
18+
var express = require('express');
19+
var request = require('request');
20+
21+
var app = express();
22+
23+
// The following environment variable is set by app.yaml when running on GAE,
24+
// but will need to be manually set when running locally. See README.md.
25+
var GA_TRACKING_ID = process.env.GA_TRACKING_ID;
26+
27+
function trackEvent(category, action, label, value, cb) {
28+
var data = {
29+
v: '1', // API Version.
30+
tid: GA_TRACKING_ID, // Tracking ID / Property ID.
31+
// Anonymous Client Identifier. Ideally, this should be a UUID that
32+
// is associated with particular user, device, or browser instance.
33+
cid: '555',
34+
t: 'event', // Event hit type.
35+
ec: category, // Event category.
36+
ea: action, // Event action.
37+
el: label, // Event label.
38+
ev: value, // Event value.
39+
};
40+
41+
request.post(
42+
'http://www.google-analytics.com/collect', {
43+
form: data
44+
},
45+
function(err, response) {
46+
if (err) { return cb(err); }
47+
if (response.statusCode !== 200) {
48+
return cb(new Error('Tracking failed'));
49+
}
50+
cb();
51+
}
52+
);
53+
}
54+
55+
app.get('/', function(req, res, next) {
56+
trackEvent(
57+
'Example category',
58+
'Example action',
59+
'Example label',
60+
'100', // Event value must be numeric.
61+
function(err) {
62+
// This sample treats an event tracking error as a fatal error. Depending
63+
// on your application's needs, failing to track an event may not be
64+
// considered an error.
65+
if (err) { return next(err); }
66+
res.status(200).send('Event tracked.');
67+
});
68+
});
69+
70+
// Start the server
71+
var server = app.listen(process.env.PORT || '8080', '0.0.0.0', function() {
72+
console.log('App listening at http://%s:%s', server.address().address,
73+
server.address().port);
74+
console.log('Press Ctrl+C to quit.');
75+
});
76+
// [END app]

appengine/analytics/app.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2015-2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
18+
# [START env]
19+
env_variables:
20+
GA_TRACKING_ID: <your-tracking-id>
21+
# [END env]
22+
23+
skip_files:
24+
- ^(.*/)?.*/node_modules/.*$
25+
# [END app_yaml]

0 commit comments

Comments
 (0)