Skip to content
Permalink
Browse files

Started adding RESTful methods for Document

  • Loading branch information...
Alex Young
Alex Young committed Nov 15, 2010
1 parent 4ea936b commit 39e66cb9d11a67044495beb0de1934ac4d9c4786
Showing with 110 additions and 9 deletions.
  1. +57 −3 app.js
  2. +3 −2 models.js
  3. +50 −4 test/app.test.js
60 app.js
@@ -1,8 +1,8 @@
var express = require('express'),
app = module.exports = express.createServer(),
mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/nodepad'),
Document = require('./models.js').Document(db);
db,
Document;

app.configure(function() {
app.set('views', __dirname + '/views');
@@ -14,13 +14,24 @@ app.configure(function() {
});

app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.logger({ format: ':method :uri' }));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
db = mongoose.connect('mongodb://localhost/nodepad-development');
});

app.configure('production', function() {
app.use(express.logger());
app.use(express.errorHandler());
db = mongoose.connect('mongodb://localhost/nodepad-production');
});

app.configure('test', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
db = mongoose.connect('mongodb://localhost/nodepad-test');
});

app.Document = Document = require('./models.js').Document(db);

app.get('/', function(req, res) {
res.render('index.jade', {
locals: {
@@ -29,6 +40,49 @@ app.get('/', function(req, res) {
});
});

// Document list
app.get('/documents.:format', function(req, res) {
Document.find().all(function(documents) {
switch (req.params.format) {
case 'json':
res.send(documents.map(function(d) {
return d.__doc;
}));
break;

default:
res.render('documents/index.jade');
}
});
});

// Create document
app.post('/documents.:format?', function(req, res) {
var document = new Document(req.body['document']);
document.save(function() {
switch (req.params.format) {
case 'json':
res.send(document.__doc);
break;

default:
res.redirect('/documents');
}
});
});

// Read document
app.get('/documents/:id.:format?', function(req, res) {
});

// Update document
app.put('/documents/:id.:format?', function(req, res) {
});

// Delete document
app.del('/documents/:id.:format?', function(req, res) {
});

if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d, environment: %s", app.address().port, app.settings.env)
@@ -1,10 +1,11 @@
var mongoose = require('mongoose').Mongoose;

mongoose.model('Document', {
properties: ['title', 'data', 'tags'],
properties: ['title', 'data', 'tags', 'user_id'],

indexes: [
'title'
'title',
'user_id'
]
});

@@ -1,13 +1,59 @@

// Run $ expresso

/**
* Module dependencies.
*/
// Force test environment
process.env.NODE_ENV = 'test';

var app = require('../app');
var app = require('../app'),
lastID = '';

module.exports = {
'POST /documents.json': function(assert) {
assert.response(app, {
url: '/documents.json',
method: 'POST',
data: JSON.stringify({ document: { title: 'Test' } }),
headers: { 'Content-Type': 'application/json' }
}, {
status: 200,
headers: { 'Content-Type': 'application/json' }
},

function(res) {
var document = JSON.parse(res.body);
assert.equal('Test', document.title);
lastID = document._id;
});
},

'HTML POST /documents': function(assert) {
assert.response(app, {
url: '/documents',
method: 'POST',
data: 'document[title]=test',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}, {
status: 302,
headers: { 'Content-Type': 'text/plain' }
});
},

'GET /documents.json': function(assert) {
assert.response(app,
{ url: '/documents.json' },
{ status: 200, headers: { 'Content-Type': 'application/json' }},
function(res) {
var documents = JSON.parse(res.body);
assert.type(documents, 'object')

documents.forEach(function(d) {
app.Document.findById(d._id, function(document) {
document.remove();
})
});
});
},

'GET /': function(assert) {
assert.response(app,
{ url: '/' },

0 comments on commit 39e66cb

Please sign in to comment.
You can’t perform that action at this time.