-
-
Save DannyMcwaves/d226206503f89fce8ed953a97c00ead3 to your computer and use it in GitHub Desktop.
Tutorial: REST API with Node.js and MongoDB using Mongoskin and Express.js
This file contains hidden or 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
var express = require('express') | |
, mongoskin = require('mongoskin') | |
var app = express() | |
app.use(express.bodyParser()) | |
var db = mongoskin.db('localhost:27017/test', {safe:true}); | |
app.param('collectionName', function(req, res, next, collectionName){ | |
req.collection = db.collection(collectionName) | |
return next() | |
}) | |
app.get('/', function(req, res) { | |
res.send('please select a collection, e.g., /collections/messages') | |
}) | |
app.get('/collections/:collectionName', function(req, res) { | |
req.collection.find({},{limit:10, sort: [['_id',-1]]}).toArray(function(e, results){ | |
if (e) return next(e) | |
res.send(results) | |
}) | |
}) | |
app.post('/collections/:collectionName', function(req, res) { | |
req.collection.insert(req.body, {}, function(e, results){ | |
if (e) return next(e) | |
res.send(results) | |
}) | |
}) | |
app.get('/collections/:collectionName/:id', function(req, res) { | |
req.collection.findOne({_id: req.collection.id(req.params.id)}, function(e, result){ | |
if (e) return next(e) | |
res.send(result) | |
}) | |
}) | |
app.put('/collections/:collectionName/:id', function(req, res) { | |
req.collection.update({_id: req.collection.id(req.params.id)}, {$set:req.body}, {safe:true, multi:false}, function(e, result){ | |
if (e) return next(e) | |
res.send((result===1)?{msg:'success'}:{msg:'error'}) | |
}) | |
}) | |
app.del('/collections/:collectionName/:id', function(req, res) { | |
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'}) | |
}) | |
}) | |
app.listen(3000) |
This file contains hidden or 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
var superagent = require('superagent') | |
var expect = require('expect.js') | |
describe('express rest api server', function(){ | |
var id | |
it('post object', function(done){ | |
superagent.post('http://localhost:3000/collections/test') | |
.send({ name: 'John' | |
, email: '[email protected]' | |
}) | |
.end(function(e,res){ | |
// console.log(res.body) | |
expect(e).to.eql(null) | |
expect(res.body.length).to.eql(1) | |
expect(res.body[0]._id.length).to.eql(24) | |
id = res.body[0]._id | |
done() | |
}) | |
}) | |
it('retrieves an object', function(done){ | |
superagent.get('http://localhost:3000/collections/test/'+id) | |
.end(function(e, res){ | |
// console.log(res.body) | |
expect(e).to.eql(null) | |
expect(typeof res.body).to.eql('object') | |
expect(res.body._id.length).to.eql(24) | |
expect(res.body._id).to.eql(id) | |
done() | |
}) | |
}) | |
it('retrieves a collection', function(done){ | |
superagent.get('http://localhost:3000/collections/test') | |
.end(function(e, res){ | |
// console.log(res.body) | |
expect(e).to.eql(null) | |
expect(res.body.length).to.be.above(0) | |
expect(res.body.map(function (item){return item._id})).to.contain(id) | |
done() | |
}) | |
}) | |
it('updates an object', function(done){ | |
superagent.put('http://localhost:3000/collections/test/'+id) | |
.send({name: 'Peter' | |
, email: '[email protected]'}) | |
.end(function(e, res){ | |
// console.log(res.body) | |
expect(e).to.eql(null) | |
expect(typeof res.body).to.eql('object') | |
expect(res.body.msg).to.eql('success') | |
done() | |
}) | |
}) | |
it('checks an updated object', function(done){ | |
superagent.get('http://localhost:3000/collections/test/'+id) | |
.end(function(e, res){ | |
// console.log(res.body) | |
expect(e).to.eql(null) | |
expect(typeof res.body).to.eql('object') | |
expect(res.body._id.length).to.eql(24) | |
expect(res.body._id).to.eql(id) | |
expect(res.body.name).to.eql('Peter') | |
done() | |
}) | |
}) | |
it('removes an object', function(done){ | |
superagent.del('http://localhost:3000/collections/test/'+id) | |
.end(function(e, res){ | |
// console.log(res.body) | |
expect(e).to.eql(null) | |
expect(typeof res.body).to.eql('object') | |
expect(res.body.msg).to.eql('success') | |
done() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment