Skip to content

Commit

Permalink
Merge pull request #27 from thesandlord/master
Browse files Browse the repository at this point in the history
Added MongoDB example
  • Loading branch information
jmdobry committed Nov 19, 2015
2 parents 57d4486 + fd717a1 commit 078d5f8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -109,7 +110,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"
},
"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);

// 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;

// 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 + '; ';
});

res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end(iplist);
});
})
}).listen(process.env.PORT || 8080);;
console.log('started web process');
});

0 comments on commit 078d5f8

Please sign in to comment.