-
Notifications
You must be signed in to change notification settings - Fork 12
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 #2 from ATMartin/tiny-auth
Add simple auth
- Loading branch information
Showing
11 changed files
with
308 additions
and
97 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
.DS_Store | ||
.DS_Store | ||
npm-debug.log | ||
*.swp |
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
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
Binary file not shown.
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,58 @@ | ||
var express = require('express'), | ||
mongoskin = require('mongoskin'), | ||
router = express.Router(), | ||
db = mongoskin.db((process.env.MONGOLAB_URI || 'localhost:27017/test'), {safe: true}); | ||
|
||
// Thanks to http://webapplog.com/tutorial-node-js-and-mongodb-json-rest-api-server-with-mongoskin-and-express-js/ | ||
// for the cool help | ||
|
||
router.param('collectionName', function(req, res, next, collectionName) { | ||
req.collection = db.collection(collectionName); | ||
next(); | ||
}); | ||
|
||
router.route('/:collectionName') | ||
|
||
// GET /collections/:collectionName | ||
.get(function(req, res, next) { | ||
req.collection.find({},{limit:10, sort: [['_id',-1]]}).toArray(function(e, results){ | ||
if (e) { return next(e); } | ||
res.send(results); | ||
}); | ||
}) | ||
|
||
// POST /collections/:collectionName | ||
.post(function(req, res, next) { | ||
req.collection.insert(req.body, {}, function(e, results, next){ | ||
if (e) { return next(e); } | ||
res.send(results[0]); | ||
}); | ||
}); | ||
|
||
router.route('/:collectionName/:id') | ||
|
||
// GET /collections/:collectionName/:id | ||
.get(function(req, res, next) { | ||
req.collection.findOne({_id: req.collection.id(req.params.id)}, function(e, result){ | ||
if (e) { return next(e); } | ||
res.send(result) | ||
}) | ||
}) | ||
|
||
// PUT /collections/:collectionName/:id | ||
.put(function(req, res) { | ||
delete req.body._id; //<-- backbone sends the _id in the payload, but mongo doesn't wan it in the $set (--@masondesu) | ||
req.collection.update({_id: req.collection.id(req.params.id)}, {$set:req.body}, {safe:true, multi:false}, function(e, result){ | ||
res.send((result===1)? 200 : 404 ) | ||
}) | ||
}) | ||
|
||
// DELETE /collections/:collectionName | ||
.delete(function(req, res, next) { | ||
req.collection.remove({_id: req.collection.id(req.params.id)}, function(e, result){ | ||
if (e) { return next(e); } | ||
res.send((result===1)?{msg:'success'}:{msg:'error'}); | ||
}); | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.