Skip to content

Commit

Permalink
Add simple sample and usage with MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
sspeiche committed Jun 19, 2015
1 parent 9d30467 commit ddeacad
Show file tree
Hide file tree
Showing 3 changed files with 391 additions and 57 deletions.
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@
"description": "Node.js sample app for OpenShift 3",
"main": "server.js",
"dependencies": {
"express": "*"
},
"devDependencies": {
"nodemon": "*"
"express": "*",
"mongodb": "*",
"ejs": "*"
},
"engine": {
"node": "*",
"npm": "*"
},
"scripts": {
"dev": "nodemon --ignore node_modules/ server.js",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "http://github.com/openshift/nodejs-ex.git"
},
"keywords": [
"Echo"
],
"author": "Steve Speicher <sspeiche@gmail.com>",
"license": "",
"bugs": {
Expand Down
131 changes: 82 additions & 49 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,86 @@
var util = require('util');
var url = require('url');
var qs = require('querystring');
var os = require('os')
var port = process.env.PORT || process.env.port || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip = process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
var nodeEnv = process.env.NODE_ENV || 'unknown';

// OpenShift sample Node application
var express = require('express');
var app = express();

app.use(function(req, res, next) {
var url_parts = url.parse(req.url, true);

var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var formattedBody = qs.parse(body);

res.writeHead(200, {'Content-Type': 'text/plain'});

res.write('Hello OpenShift World! This is a Node.js-based sample application.\n');
res.write('Host: ' + req.headers.host + '\n');
res.write('\n');
res.write('node.js Production Mode: ' + (nodeEnv == 'production' ? 'yes' : 'no') + '\n');
res.write('\n');
res.write('HTTP/' + req.httpVersion +'\n');
res.write('Request headers:\n');
res.write(util.inspect(req.headers, null) + '\n');
res.write('Request query:\n');
res.write(util.inspect(url_parts.query, null) + '\n');
res.write('Request body:\n');
res.write(util.inspect(formattedBody, null) + '\n');
res.write('\n');
res.write('Host: ' + os.hostname() + '\n');
res.write('OS Type: ' + os.type() + '\n');
res.write('OS Platform: ' + os.platform() + '\n');
res.write('OS Arch: ' + os.arch() + '\n');
res.write('OS Release: ' + os.release() + '\n');
res.write('OS Uptime: ' + os.uptime() + '\n');
res.write('OS Free memory: ' + os.freemem() / 1024 / 1024 + 'mb\n');
res.write('OS Total memory: ' + os.totalmem() / 1024 / 1024 + 'mb\n');
res.write('OS CPU count: ' + os.cpus().length + '\n');
res.write('OS CPU model: ' + os.cpus()[0].model + '\n');
res.write('OS CPU speed: ' + os.cpus()[0].speed + 'mhz\n');
res.end('\n');
});
next();
var fs = require('fs');
var app = express();
var eps = require('ejs');

app.engine('html', require('ejs').renderFile);

var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
var mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL;
var mongoURLLabel = "";
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase();
var mongoHost = process.env[mongoServiceName + "_SERVICE_HOST"];
var mongoPort = process.env[mongoServiceName + "_SERVICE_PORT"];
var mongoUser = process.env.MONGODB_USER
if (mongoHost && mongoPort && process.env.MONGODB_DATABASE) {
mongoURLLabel = mongoURL = 'mongodb://';
if (process.env.MONGODB_USER && process.env.MONGODB_PASSWORD) {
mongoURL += process.env.MONGODB_USER + ':' + process.env.MONGODB_PASSWORD + '@';
}
// Provide UI label that excludes user id and pw

mongoURLLabel += mongoHost + ':' + mongoPort + '/' + process.env.MONGODB_DATABASE;
mongoURL += mongoHost + ':' + mongoPort + '/' + process.env.MONGODB_DATABASE;
}
}
var db = null;
var dbDetails = new Object();

var initDb = function(callback) {
if (mongoURL == null) return;

var mongodb = require('mongodb');
if (mongodb == null) return;

mongodb.connect(mongoURL, function(err, conn) {
if (err) {
callback(err);
return;
}

db = conn;
dbDetails.databaseName = db.databaseName;
dbDetails.url = mongoURLLabel;
dbDetails.type = 'MongoDB';

console.log("Connected to MongoDB at: " + mongoURL);
});
};

app.get('/', function (req, res) {
if (db) {
var col = db.collection('counts');
// Create a document with request IP and current time of request
col.insert({ip: req.ip, date: Date.now()});
col.count(function(err, count){
res.render('index.html', { pageCountMessage : count, dbInfo: dbDetails });
});
} else {
res.render('index.html', { pageCountMessage : null});
}
});

app.get('/pagecount', function (req, res) {
if (db) {
db.collection('counts').count(function(err, count ){
res.send('{ pageCount: ' + count +'}');
});
} else {
res.send('{ pageCount: -1 }');
}
});

// error handling
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something bad happened!');
});

initDb(function(err){
console.log('Error connecting to Mongo. Message:\n'+err);
});

app.listen(port, ip);
Expand Down
Loading

0 comments on commit ddeacad

Please sign in to comment.