diff --git a/README.md b/README.md index b8813c2dbe..a6f79410a3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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/#/ diff --git a/appengine/mongo/.gitignore b/appengine/mongo/.gitignore new file mode 100644 index 0000000000..34afe36bca --- /dev/null +++ b/appengine/mongo/.gitignore @@ -0,0 +1 @@ +keys.json diff --git a/appengine/mongo/README.md b/appengine/mongo/README.md new file mode 100644 index 0000000000..9687e4099e --- /dev/null +++ b/appengine/mongo/README.md @@ -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/ diff --git a/appengine/mongo/app.yaml b/appengine/mongo/app.yaml new file mode 100644 index 0000000000..7176b385a4 --- /dev/null +++ b/appengine/mongo/app.yaml @@ -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] \ No newline at end of file diff --git a/appengine/mongo/package.json b/appengine/mongo/package.json new file mode 100644 index 0000000000..39f6bdea8d --- /dev/null +++ b/appengine/mongo/package.json @@ -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" + } +} diff --git a/appengine/mongo/server.js b/appengine/mongo/server.js new file mode 100644 index 0000000000..052f3cad25 --- /dev/null +++ b/appengine/mongo/server.js @@ -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'); +});