-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from thesandlord/master
Added MongoDB example
- Loading branch information
Showing
6 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
keys.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |