Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions appengine-standard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Google App Engine Node.js Samples

These are samples for using [Node.js][nodejs] on
Google App Engine Standard Environment.

There are also samples [submitted by the community][community_samples].

See our other [Google Cloud Platform GitHub repositories](/GoogleCloudPlatform)
for sample applications and scaffolding for other frameworks and use cases.

* [Run Locally](#run-locally)
* [Deploying](#deploying)
* [Official samples](#official-samples)
* [Community samples](#community-samples)

## Run Locally

Some samples have specific instructions. If there is a `README.md` file in the
sample folder, please refer to it for any additional steps required to run the
sample.

The App Engine Node.js samples typically that you do the following:

1. [Setup your environment for Node.js developement][nodejs_dev].
1. [Install the Google Cloud SDK][sdk].
1. Acquire local credentials for authenticating with Google Cloud Platform APIs:

gcloud auth application-default login

1. Clone this repo:

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

1. Choose a sample:

cd appengine-standard/sample-folder/

1. Install depedencies using `npm` or `yarn`:

npm install

or

yarn install

1. Run the sample with `npm` or `yarn` (See the sample's `README.md` file for
any additional setup):

npm start

or

yarn start

1. Visit the application at [http://localhost:8080][].

## Deploying

Some samples in this repositories may have special deployment instructions.
Refer to the `README.md` file in the sample folder.

1. Use the [Google Cloud Console][console] to create a Google Cloud Platform
project.
1. [Enable billing][billing] for your project.

1. Use the Cloud SDK to deploy your app.

gcloud app deploy

Note: If there is a `yarn.lock` file then `yarn install` will be used during
deployment. Delete the `yarn.lock` file to fall back to `npm install`.

1. View your deployed application at `https://YOUR_PROJECT_ID.appspot.com`.

## Official samples

View the [Official App Engine Node.js samples][official_samples].

## Community samples

View the [Community-contributed App Engine Node.js samples][community_samples].

[nodejs]: https://nodejs.org/
[appengine]: https://cloud.google.com/appengine/docs/flexible/nodejs/
[nodejs_dev]: https://cloud.google.com/community/tutorials/how-to-prepare-a-nodejs-dev-environment
[sdk]: https://cloud.google.com/sdk/
[console]: https://console.cloud.google.com
[billing]: https://support.google.com/cloud/answer/6293499#enable-billing
[official_samples]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine
[community_samples]: https://cloud.google.com/community/tutorials/?q=%22Node.js%22
55 changes: 55 additions & 0 deletions appengine-standard/analytics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Integrating with Google Analytics

This sample application demonstrates how to integrate Google App Engine Node.js
Standard Environment with Google Analytics.

* [Setup](#setup)
* [Running locally](#running-locally)
* [Deploying to App Engine](#deploying-to-app-engine)
* [Running the tests](#running-the-tests)

## Setup

Before you can run or deploy the sample, you need to do the following:

1. Refer to the [appengine-standard/README.md][readme] file for instructions on
running and deploying.
1. [Create a Google Analytics Property and obtain the Tracking ID][tracking].
1. Add your tracking ID to `app.yaml`.
1. Install dependencies:

With `npm`:

npm install

or with `yarn`:

yarn install

## Running locally

With `npm`:

GA_TRACKING_ID=YOUR_TRACKING_ID npm start

or with `yarn`:

GA_TRACKING_ID=YOUR_TRACKING_ID yarn start

## Deploying to App Engine

With `npm`:

npm run deploy

or with `yarn`:

yarn run deploy

## Running the tests

See [Contributing][contributing].

[readme]: ../README.md
[tracking]: https://support.google.com/analytics/answer/1042508
[contributing]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/CONTRIBUTING.md
72 changes: 72 additions & 0 deletions appengine-standard/analytics/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START app]
const express = require('express');
const got = require('got');

const app = express();
app.enable('trust proxy');

// The following environment variable is set by app.yaml when running on App
// Engine, but will need to be set manually when running locally. See README.md.
const GA_TRACKING_ID = process.env.GA_TRACKING_ID;

function trackEvent (category, action, label, value, cb) {
const data = {
// API Version.
v: '1',
// Tracking ID / Property ID.
tid: GA_TRACKING_ID,
// Anonymous Client Identifier. Ideally, this should be a UUID that
// is associated with particular user, device, or browser instance.
cid: '555',
// Event hit type.
t: 'event',
// Event category.
ec: category,
// Event action.
ea: action,
// Event label.
el: label,
// Event value.
ev: value
};

return got.post('http://www.google-analytics.com/collect', {
form: data
});
}

app.get('/', (req, res, next) => {
// Event value must be numeric.
trackEvent('Example category', 'Example action', 'Example label', '100')
.then(() => {
res.status(200).send('Event tracked.').end();
})
// This sample treats an event tracking error as a fatal error. Depending
// on your application's needs, failing to track an event may not be
// considered an error.
.catch(next);
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END app]
21 changes: 21 additions & 0 deletions appengine-standard/analytics/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app_yaml]
runtime: nodejs8

# [START env]
env_variables:
GA_TRACKING_ID: YOUR_TRACKING_ID
# [END env]
# [END app_yaml]
40 changes: 40 additions & 0 deletions appengine-standard/analytics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "appengine-analytics",
"description": "Sample for Google Analytics Measurement Protocol on Google App Engine Standard Environment.",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": "8.x.x"
},
"scripts": {
"deploy": "gcloud app deploy",
"start": "node app.js",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"system-test": "repo-tools test app",
"test": "npm run system-test",
"e2e-test": "repo-tools test deploy"
},
"dependencies": {
"express": "4.16.0",
"got": "7.1.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "2.0.4"
},
"cloud-repo-tools": {
"test": {
"app": {
"msg": "Event tracked."
}
},
"requiresKeyFile": true,
"requiresProjectId": true
}
}
8 changes: 8 additions & 0 deletions appengine-standard/cloudsql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Cloud SQL for MySQL Node.js sample on App Engine Standard environment

This sample demonstrates how to use [Google Cloud SQL][sql] for
[MySQL][mysql] on Google App Engine Standard Environment.

[sql]: https://cloud.google.com/sql/
[flexible]: https://cloud.google.com/appengine
[mysql]: https://www.mysql.com/downloads/
33 changes: 33 additions & 0 deletions appengine-standard/cloudsql/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2017, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START app_yaml]
runtime: nodejs8

# [START env]
env_variables:
SQL_USER: YOUR_SQL_USER
SQL_PASSWORD: YOUR_SQL_PASSWORD
SQL_DATABASE: YOUR_SQL_DATABASE
# e.g. my-awesome-project:us-central1:my-cloud-sql-instance
INSTANCE_CONNECTION_NAME: YOUR_INSTANCE_CONNECTION_NAME
# [END env]

# [START cloudsql_settings]
beta_settings:
# The connection name of your instance, available by using
# 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
# the Instance details page in the Google Cloud Platform Console.
cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME
# [END cloudsql_settings]
# [END app_yaml]
53 changes: 53 additions & 0 deletions appengine-standard/cloudsql/createTables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START createTables]
const Knex = require('knex');
const prompt = require('prompt');

const FIELDS = ['user', 'password', 'database'];

prompt.start();

// Prompt the user for connection details
prompt.get(FIELDS, (err, config) => {
if (err) {
console.error(err);
return;
}

// Connect to the database
const knex = Knex({ client: 'mysql', connection: config });

// Create the "visits" table
knex.schema.createTable('visits', (table) => {
table.increments();
table.timestamp('timestamp');
table.string('userIp');
})
.then(() => {
console.log(`Successfully created 'visits' table.`);
return knex.destroy();
})
.catch((err) => {
console.error(`Failed to create 'visits' table:`, err);
if (knex) {
knex.destroy();
}
});
});
// [END createTables]
Loading