-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
john@traider.io
committed
Apr 23, 2014
0 parents
commit 929a497
Showing
6 changed files
with
163 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var db = require("../db/db.products.js"); | ||
|
||
// define the routes for /api/users | ||
module.exports = function attachHandlers (router){ //, passport) { | ||
// get requests | ||
router.get('/api/products', list); | ||
router.get('/api/products/:id', view); | ||
}; | ||
|
||
|
||
function list(req, res) | ||
{ | ||
db.getAll(function(data){ | ||
res.json(data); | ||
}); | ||
} | ||
|
||
function view(req, res) | ||
{ | ||
db.getById(req.params.id, function(data){ | ||
res.json(data); | ||
}); | ||
} |
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 @@ | ||
var mongo = require("mongodb"); | ||
var MongoClient = mongo.MongoClient, | ||
Server = require('mongodb').Server, | ||
BSON = mongo.BSONPure; | ||
|
||
var mongoclient = new MongoClient(new Server("127.0.0.1", 27017), {native_parser: true}); | ||
|
||
exports.getDbClient = function(){ | ||
return mongoclient; | ||
}; | ||
|
||
exports.dbName = function(){ | ||
return "traider"; | ||
}; | ||
|
||
exports.makeObjectID = function(id){ | ||
return new BSON.ObjectID(id); | ||
}; |
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,65 @@ | ||
var mongoHandler = require("./db.client.js"); | ||
var mongoclient = mongoHandler.getDbClient(); | ||
var collectionName = "products"; | ||
|
||
|
||
exports.getById = function(id, callback){ | ||
|
||
// Open the connection to the server | ||
mongoclient.open(function(err, mongoclient) { | ||
var dbName = mongoHandler.dbName(); | ||
var db = mongoclient.db(dbName); | ||
|
||
db.collection(collectionName).findOne({"_id": mongoHandler.makeObjectID(id)}, function(err, result) { | ||
mongoclient.close(); | ||
if (err){ | ||
mongoclient.close(); | ||
throw err.Message; | ||
return; | ||
} | ||
else | ||
{ | ||
// Close the connection | ||
callback(result); | ||
return; | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
exports.getAll = function(callback) | ||
{ | ||
if(callback === null || typeof(callback) !== "function"){throw "Call to db method must include callback function"} | ||
mongoclient.open(function(err, mongoclient) { | ||
|
||
if (err){ | ||
mongoclient.close(); | ||
throw err.Message; | ||
return; | ||
} | ||
|
||
var dbName = mongoHandler.dbName(); | ||
var db = mongoclient.db(dbName); | ||
console.log(dbName+"."+collectionName); | ||
|
||
db.collection(collectionName).find({}, function(err, result) { | ||
if (err){ | ||
mongoclient.close(); | ||
throw err.Message; | ||
return; | ||
} | ||
else | ||
{ | ||
result.toArray(function(err, resultArray) | ||
{ | ||
// Close the connection | ||
mongoclient.close(); | ||
|
||
console.log("Got data: " + resultArray.length + " records."); | ||
callback(resultArray); | ||
return; | ||
}); | ||
} | ||
}); | ||
}); | ||
}; |
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,25 @@ | ||
{ | ||
"name": "traider.io", | ||
"description": "", | ||
"version": "0.0.1", | ||
"main": "traider.js", | ||
"author": { | ||
"name": "john", | ||
"email": "john@traider.io" | ||
}, | ||
"keywords": [], | ||
"licenses": { | ||
"type": "mit" | ||
}, | ||
"dependencies": { | ||
"express": "3.x" , | ||
"mongodb": "1.3.23" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:EastpointSoftware/traider.io.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/EastpointSoftware/traider.io/issues" | ||
} | ||
} |
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,7 @@ | ||
|
||
|
||
exports.attachHandlers = function attachHandlers (server){ //, passport) { | ||
|
||
require('../api/api.products.js')(server); //, passport); | ||
|
||
}; |
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,25 @@ | ||
var express = require('express'); | ||
var routes = require('./routes/routes.js'); | ||
|
||
|
||
createServer = function createServer () { | ||
|
||
var server = express(); | ||
// specify middleware | ||
//server.use(express.cookieParser()); | ||
|
||
//server.use(express.bodyParser()); | ||
server.use('/css', express.static(__dirname + '/media/css')); | ||
server.use('/img', express.static(__dirname + '/media/img')); | ||
server.use('/js', express.static(__dirname + '/media/js')); | ||
|
||
// attach router handlers | ||
routes.attachHandlers(server); //, passport); | ||
|
||
return server; | ||
|
||
}; | ||
|
||
|
||
var server = createServer(); | ||
server.listen(8003); |