Skip to content

Instantly share code, notes, and snippets.

@adg29
Forked from simenbrekken/README.md
Last active April 27, 2017 17:58
Show Gist options
  • Save adg29/4614371 to your computer and use it in GitHub Desktop.
Save adg29/4614371 to your computer and use it in GitHub Desktop.
Simple Backbone + Express + MongoDB REST backend application

Requirements:

If you're on OSX you're probably best off using Homebrew to install this stuff:

$ brew install node mongodb

Usage:

$ npm install express mongoose
$ node backend.js

I've also attached a sample frontend code for using the backend via Backbone

I highly recommend the Postman Chrome extension for experimenting with REST APIs in which case you can import the attached collection export.

var express = require('express'),
mongoq = require('mongoose');
var db = mongoose.connect('mongodb://localhost/test-database'); // Initialize database
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var Score = new Schema({
name : { type: String, default: 'anonymous' }
, points : { type: Number }
, date : { type: Date, default: Date.now }
});
var app = express.createServer();
app.use(express.bodyParser()); // Automatically parse JSON in POST requests
app.use(express.static(__dirname + '/public')); // Serve static files from public (e.g http://localhost:8080/index.html)
app.use(express.errorHandler({dumpExceptions: true, showStack: true})); // Dump errors
// Add score
app.post('/api/scores', function(req, res) {
var score = req.body;
//score.id = Date.now().toString(); // You probably want to swap this for something like https://github.com/dylang/shortid
// retrieve my model
var ScoreModel = mongoose.model('Score')
, scorePost = new ScoreModel();
// create an embedded score (given an embedUmbrella schema instance)
//embedUmbrella.scorePost.push(score);
scorePost.push(score);
scorePost.save(function (err) {
if (!err) console.log('Success!');
res.json(score, 201);
});
});
// List scores (accepts skip and limit query parameters)
app.get('/api/scores', function(req, res) {
db.collection('scores').find().skip(req.query.skip || 0).limit(req.query.limit || 0).toArray().done(function(scores) {
res.json(scores);
});
});
// Read score
app.get('/api/scores/:id', function(req, res) {
db.collection('scores').findOne({id: req.params.id}).done(function(score) {
res.json(score);
});
});
// Update score (supports partial updates, e.g only send one field or all)
app.put('/api/scores/:id', function(req, res) {
var score = req.body;
db.collection('scores').update({id: req.params.id}, {$set: score}, {safe: true}).done(function(success) {
res.json(success ? 200 : 404);
});
});
// Delete score
app.del('/api/scores/:id', function(req, res) {
db.collection('scores').remove({id: req.params.id}, {safe: true}).done(function(success) {
res.json(success ? 200 : 404);
});
});
app.listen(8080);
var scores = new Backbone.Collection();
scores.url = 'http://localhost:8080/api/scores';
scores.fetch(); // Executes a HTTP GET to get scores from server
var score = new Backbone.Model();
score.set({'points': 1337, 'name': 'Foo'});
scores.add(score); // Executes a HTTP POST to create a new score
score.set({'name': 'Bar'}); // Executes a HTTP PUT to update the score
score.destroy(); // Executes a HTTP DELETE to remove the score
{
"id": "a62b331e-61c9-8891-7f60-abf135c54a9d",
"name": "Scores",
"timestamp": 1331189268220,
"requests": [{
"collectionId": "a62b331e-61c9-8891-7f60-abf135c54a9d",
"id": "71912c90-f23a-f4e0-a822-c9a4402c475f",
"name": "Delete score",
"description": "",
"url": "http://localhost:8080/api/scores/1331189244092",
"method": "delete",
"headers": "",
"data": "",
"dataMode": "raw",
"timestamp": 0
}, {
"collectionId": "a62b331e-61c9-8891-7f60-abf135c54a9d",
"id": "bc36c8d7-ff98-6ceb-31ee-b25524600b57",
"name": "Create dummy score",
"description": "",
"url": "http://localhost:8080/api/scores",
"method": "post",
"headers": "Content-Type: application/json\n",
"data": "{\"points\": 1337, \"name\": \"Foo\"}",
"dataMode": "raw",
"timestamp": 0
}, {
"collectionId": "a62b331e-61c9-8891-7f60-abf135c54a9d",
"id": "c1884e4f-fc23-8242-992e-d027563227c3",
"name": "Update score",
"description": "",
"url": "http://localhost:8080/api/scores/1331189866741",
"method": "put",
"headers": "Content-Type: application/json\n",
"data": "{\n\t\t\"points\": 456,\n\t\t\"name\": \"Bar\"\n}",
"dataMode": "raw",
"timestamp": 0
}, {
"collectionId": "a62b331e-61c9-8891-7f60-abf135c54a9d",
"id": "dfc4c181-e4d3-31fe-144b-eac2adf0278d",
"name": "Get scores",
"description": "",
"url": "http://localhost:8080/api/scores",
"method": "get",
"headers": "",
"data": "",
"dataMode": "raw",
"timestamp": 0
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment