PLOSDB reworked with mongodb and node

PLOSDB reworked with mongodb and node

It's been super easy integrating node with mongodb to create a PLOSDB server. Since all of the assets are static with PLOSDB, a document store made sense. I'm still looking for a more reactive server to use for super agile data sets like player locations and stuff, but that can be set aside for now while I work on Clay. Here was the basic code for setting up the server as well as one of the routes.

var instance = require('./instance.js'),  
    db = instance.db,
    app = instance.app;

/**
 * Initialize the database connection
 */
db.open(function(err, db) {  
  if(!err) {
    console.log("Connected to 'plos' database");
  } //end if
});

/**
 * Create all of the routes for the api
 */
require('./routes/emotes');  
require('./routes/abilities');  
require('./routes/classes');  
require('./routes/items');  
require('./routes/mobiles');  
require('./routes/help');  
require('./routes/races');  
require('./routes/locations');

/**
 * Initialize the api by listening for incoming connections
 */
app.listen(5000);  
console.log('Listening on port 5000...');  
var mongo   = require('mongodb'),  
    express = require('express'),
    app     = express(),
    cors    = require('cors'),
    Server  = mongo.Server,
    Db      = mongo.Db,
    server  = new Server('localhost', 27017, {auto_reconnect: true});

/**
 * Ensure that we allow CORS on our server for our restful interfaces
 */
app.use(cors());

/**
 * export all of the instances that each routing module will need to use
 *
 * @type {Object}
 */
module.exports = {  
  app: app,
  ObjectId: mongo.ObjectID,
  db: new Db('plos', server)
}
var instance = require('../instance.js'),  
    app = instance.app,
    db = instance.db,
    ObjectId = instance.ObjectId,
    async = require('async');

/**
 * The main collection of abilities which is used to setting up the sub-routes
 * 
 * @type {Array}
 */
var abilities = [  
  'alchemy',
  'animalHusbandry',
  'blacksmithing',
  'carpentry',
  'enchanting',
  'fletchery',
  'jewelcrafting',
  'leatherworking',
  'mining',
  'tailoring'
];

/**
 * Asynchronously iterate through all of the abilities collections and
 * concatenate all the results together and feed it back to the requestor.
 * Note that if any errors occur, they are deffered and console logged, no errors
 * are fed back to the user.
 */
app.get('/abilities', function(req, res) {  
  console.log('Sending all abilities...');
  async.concat(abilities,function(name,done){
    db.collection('abilities.'+name,function(err,collection){
      if(err){
        done(err,[]);
      }else{
        collection.find().toArray(function(err, items){
          done(err,items);
        });
      } //end if
    });
  },function(err,results){
    if(err){ console.log('ERROR: ',err); }
    res.send(results);
    console.log('Finished sending all abilities.');
  });
});

/**
 * Iterate through each of the individual ability files and add each one as a
 * route.
 */
abilities.forEach(function(link){  
  app.get('/abilities/'+link,function(req,res){
    db.collection('abilities.'+link,function(err,collection){
      if(err){
        res.send('[]');
      }else{
        collection.find().toArray(function(err,items){
          if(err){
            res.send('[]');
          }else{
            res.send(items);
          } //end if
        });
      } //end if
    });
  });
});

/**
 * Iterate through each of the individual ability files and add each one as a
 * route for accepting individual id requests.
 */
abilities.forEach(function(link){  
  app.get('/abilities/'+link+'/:id',function(req,res){
    var id = req.params.id;
    console.log('Sending '+link+' abilities ['+id+']...');
    db.collection('abilities.'+link,function(err,collection){
      if(err){
        res.send('[]');
      }else{
        collection.findOne({
          '_id': new ObjectId(id)
        },function(err,item){
          if(err){
            res.send('[]');
          }else{
            res.send(item);
          } //end if
        });
      } //end if
    });
  });
});

About Nathaniel Inman

I'm a gamer, graphic designer, musician and software engineer. C++ and Javascript are my favorite two programming languages for which I develop exclusively in vim, tmux and zsh on Arch Linux.

Kansas City, Missouri http://www.theoestudio.com