Skip to content

Instantly share code, notes, and snippets.

@azat-co
Last active August 19, 2018 03:30
  • Star 37 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save azat-co/6075685 to your computer and use it in GitHub Desktop.
Tutorial: REST API with Node.js and MongoDB using Mongoskin and Express.js
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)
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: 'john@rpjs.co'
})
.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: 'peter@yahoo.com'})
.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