Skip to content

Commit

Permalink
use mapReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
jollen committed Nov 16, 2014
1 parent fbfd4d9 commit b6dfa41
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ var postSchema = new mongoose.Schema({
content: String
});

var mapAgeSchema = new mongoose.Schema({
value: { type: Number, default: 0}
});

app.db = {
model: {
User: mongoose.model('user', vcardSchema),
Post: mongoose.model('post', postSchema)
Post: mongoose.model('post', postSchema),
/* MapReduce */
MapAge: mongoose.model('map_age', mapAgeSchema)
}
};

Expand All @@ -63,6 +69,7 @@ if ('development' == app.get('env')) {
}

app.get('/', require('./routes/index').index);
app.get('/post', require('./routes/index').post);

// REST API
app.post('/1/user', api.create);
Expand All @@ -73,10 +80,13 @@ app.delete('/1/user/:nickname', api.delete);
app.get('/1/user/age/:age', api.readByAge);
app.get('/1/user/age/:from/:to', api.readByAgeRange);

app.get('/1/user/map/age', api.mapByAge);

// Profile
app.post('/1/user/:nickname/:type', api.upload);

app.post('/1/post', api.createPost);
app.get('/1/post', api.readPost);

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
Expand Down
44 changes: 44 additions & 0 deletions public/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* SETUP
**/
var app = app || {};

/**
* MODELS
**/
app.Posts = Backbone.Model.extend({
url: function() {
return '/1/post';
},
defaults: {
errors: [],
errfor: {},
posts: []
}
});

/**
* VIEWS
**/
app.ListView = Backbone.View.extend({
el: '#postList',
template: _.template( $('#tmpl-post-list').html() ),
events: {
},
initialize: function() {
this.model = new app.Posts();
this.listenTo(this.model, 'sync', this.render);
this.listenTo(this.model, 'change', this.render);
this.model.fetch();
},
render: function() {
this.$el.html(this.template( this.model.attributes ));
}
});

/**
* BOOTUP
**/
$(document).ready(function() {
app.listView = new app.ListView();
});
30 changes: 30 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ exports.createPost = function(req, res){
res.send({status: 'OK'});
};

exports.readPost = function(req, res){
var model = req.app.db.model.Post;

model
.find({})
.populate('uid')
.exec(function(err, posts) {
res.send({
posts: posts
});
res.end();
});
};

exports.mapByAge = function(req, res) {
var model = req.app.db.model.User;

model
.mapReduce(
function() { emit( this.Age, this.Name); },
function(key, values) {
return values.toString();
},
{
query: { Age: { $gt: 30 } },
out: 'map_ages'
}
);
};

exports.upload = function(req, res) {

var type = req.params.type; // 'photo' or 'voice'
Expand Down
4 changes: 4 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

exports.index = function(req, res){
res.render('index', { title: 'Express' });
};

exports.post = function(req, res){
res.render('post', { title: 'Express' });
};
17 changes: 17 additions & 0 deletions views/post.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends layout

block content
h1= title
div#postList

script(type='text/template', id='tmpl-post-list')
h3 Post List
ul
<% _.each(posts, function(post) { %>
li <%= post.title %>, <%= post.uid.Name %>: <%= post.content %>
<% }); %>

script(src='jquery/dist/jquery.min.js', type='text/javascript')
script(src='underscore/underscore-min.js', type='text/javascript')
script(src='backbone/backbone.js', type='text/javascript')
script(src='post.js', type='text/javascript')

0 comments on commit b6dfa41

Please sign in to comment.