Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MongoDB example #27

Merged
merged 1 commit into from
Nov 19, 2015
Merged
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and applications on [Google App Engine](http://cloud.google.com/nodejs).
### Databases

- Redis - [Source code][redis_1] | [App Engine Tutorial][redis_2] | [Documentation][redis_3]
- MongoDB - [Source code][mongo_1] | [App Engine Tutorial][mongo_2] | [Documentation][mongo_3]

### Tools

Expand Down Expand Up @@ -106,7 +107,11 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma

[redis_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/redis
[redis_2]: https://cloud.google.com/nodejs/resources/databases/redis
[redis_3]: http://redis.io/
[redis_3]: https://redis.io/

[mongo_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/mongo
[mongo_2]: https://cloud.google.com/nodejs/resources/databases/mongo
[mongo_3]: https://docs.mongodb.org/

[gcloud_1]: https://github.com/GoogleCloudPlatform/gcloud-node
[gcloud_2]: https://googlecloudplatform.github.io/gcloud-node/#/
Expand Down
1 change: 1 addition & 0 deletions appengine/mongo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
keys.json
12 changes: 12 additions & 0 deletions appengine/mongo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## MongoDB on Google App Engine

> [MongoDB][1] is an open source (AGPL licensed), NoSQL document database.

Read the [MongoDB on App Engine Tutorial][2] for how to run and deploy this sample
app.

You can also read the [node_mongodb documentation][3].

[1]: http://mongodb.org/
[2]: https://cloud.google.com/nodejs/resources/databases/mongo
[3]: http://mongodb.github.io/node-mongodb-native/
19 changes: 19 additions & 0 deletions appengine/mongo/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2015, 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: nodejs
vm: true
env_variables:
PORT: 8080
# [END app_yaml]
18 changes: 18 additions & 0 deletions appengine/mongo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "appengine-mongo",
"description": "An example of using MongoDB with Node.js on Google App Engine.",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"engines": {
"node": "~0.12.7"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of wed, 4.2.1 is now the default version on the image. Next wed it will be 4.2.2. Maybe more of a @jmdobry question, but we should do a pass to bring this all up to 4.x.

},
"scripts": {
"start": "node server.js",
"deploy": "gcloud preview app deploy app.yaml"
},
"dependencies": {
"nconf": "^0.8.0",
"mongodb": "^2.0.48"
}
}
71 changes: 71 additions & 0 deletions appengine/mongo/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2015, 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';

var mongodb = require('mongodb');
var http = require('http');
var nconf = require('nconf');

// read in keys and secrets. You can store these in a variety of ways.
// I like to use a keys.json file that is in the .gitignore file,
// but you can also store them in environment variables
nconf.argv().env().file('keys.json');

// Connect to a MongoDB server provisioned over at
// MongoLab. See the README for more info.
var uri = 'mongodb://' +
nconf.get('mongoUser') + ':' +
nconf.get('mongoPass') + '@' +
nconf.get('mongoHost') + ':' +
nconf.get('mongoPort') + '/' +
nconf.get('mongoDatabase');

mongodb.MongoClient.connect(uri, function(err, db) {

if (err) throw err;

// Create a simple little server.
http.createServer(function(req, res) {

console.log("test");
console.log(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't need these :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, remove these console.logs


// Track every IP that has visited this site
var collection = db.collection('IPs');

var ip = {"address": req.connection.remoteAddress};

collection.insert(ip, function(err, result) {

if (err) throw err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that'll crash the process, won't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing to connect to Mongo on startup should crash the process, at least for this sample. The is consistent with how we've done things in a few other places.


// push out a range
var iplist = '';
collection.find().toArray(function(err, data) {
if (err) throw err;
console.log('listing data...\n');
data.forEach(function(ip) {
console.log('IP: ' + ip.address + '\n');
iplist += ip.address + '; ';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

});

res.writeHead(200, {
'Content-Type': 'text/plain'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more indendation

});
res.end(iplist);
});
})
}).listen(process.env.PORT || 8080);;
console.log('started web process');
});