Skip to content

Commit

Permalink
latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbeletsky committed Feb 24, 2013
0 parents commit 80342ad
Show file tree
Hide file tree
Showing 46 changed files with 52,453 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
*.swp
*.swo
node_modules
_SpecRunner.html
53 changes: 53 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"predef": [
"sinon",
"before",
"after",
"it",
"xit",
"describe",
"expect",
"beforeEach",
"afterEach",
"spyOn",
"waits",
"waitsFor",
"runs",
"_",
"sba",
"Backbone",
"jasmine",
"afterEach"
],

"node" : true,
"browser" : true,
"jquery" : true,
"boss" : false,
"curly": true,
"debug": false,
"devel": false,
"eqeqeq": true,
"evil": false,
"forin": false,
"immed": true,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"onevar": false,
"plusplus": false,
"multistr": true,
"regexp": false,
"undef": true,
"sub": false,
"strict": false,
"bitwise" : true,
"curly" : true,
"latedef" : false,
"noarg" : true,
"trailing" : true,
"asi" : false,
"expr": true
}
Empty file added README.md
Empty file.
104 changes: 104 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Implements an in-memory server for Backbone.js
// http://documentcloud.github.com/backbone/#Sync

var express = require('express'),
app = express(),
idCounter = 0,
data = {},
port = 3002,
_ = require('underscore');

app.configure(function () {
app.use(express.bodyParser());
app.use(allowCrossDomain);
app.use(express.static(__dirname + '/public'));
});

app.get('/', function(req, res) {
res.sendfile('public/index.html');
});

app.get('/tests', function(req, res) {
res.sendfile('public/tests.html');
});

app.get("/:collection", function(req, res) {
console.log('read ' + req.params.collection);
if (!data[req.params.collection]) {
return res.send(404);
}

data[req.params.collection] = data[req.params.collection] || [];
res.send(data[req.params.collection]);
});

// create -> POST /collection
app.post('/:collection', function(req, res){
console.log('create ' + req.params.collection);
console.log(req.body);
req.body.id = idCounter++;
data[req.params.collection] = data[req.params.collection] || [];
data[req.params.collection].push(req.body);
res.send({ id: req.body.id });
});

// read -> GET /collection[/id]
app.get('/:collection/:id?', function (req,res) {
console.log('read ' + (req.params.id || ('collection ' + req.params.collection)));

if (data[req.params.collection] && !req.params.id) {
return res.send(data[req.params.collection]);
}

var model = findModel(req.params.collection, req.params.id);
if (!model && req.params.id) {
return res.send('cant find model ' + req.params.id, 404);
}
res.send(model);
});

// update -> PUT /collection/id
app.put('/:collection/:id', function (req,res) {
console.log('update ' + req.params.collection + ' - ' + req.params.id);
removeModel(req.params.collection, req.params.id);
data[req.params.collection].push(req.body);
res.send(200);
});

// delete -> DELETE /collection/id
app.delete('/:collection/:id', function (req,res) {
console.log('delete ' + req.params.collection + ' - ' + req.params.id);
removeModel(req.params.collection, req.params.id);
res.send(200);
});

app.listen(port);
console.log('Backbone-server started at port %d.', port);

function removeModel(collection, id) {
if (!data[collection]) {
console.log('cant find collection ' + collection);
return;
}
data[collection] = _(data[collection]).reject(function (model) {
return model.id === parseInt(id, 10);
});
}

function findModel(collection, id) {
if (!data[collection]) {
console.log('cant find collection ' + collection);
return;
}
return _(data[collection]).find(function (m) {
return m.id === parseInt(id, 10);
});
}

function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');

next();
}
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.0rc4",
"jade": "*"
}
}

0 comments on commit 80342ad

Please sign in to comment.