Created
June 10, 2015 01:32
-
-
Save DavidSouther/a906828ae144dfa7194d to your computer and use it in GitHub Desktop.
Rupert Plugins (Mongo) example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = { | |
name: 'rupert-basic-example', | |
stassets: { | |
root: './src/client' | |
}, | |
server: { | |
root: './src/server' | |
} | |
}; | |
require('rupert')(config).start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html5> | |
<html lang="en"> | |
<head> | |
<title>Plugins Demo</title> | |
<link rel="stylesheet" href="/vendors.css"/> | |
</head> | |
<body> | |
<div class="container"> | |
<h1 class="title">Rupert Mongo Demo</h1> | |
<div> | |
<p>What's your name?</p> | |
<input id="name" class="form-control" /> | |
<p id="hello" class="bg-info"></p> | |
<h2>I have greeted</h2> | |
<ul id="greeted"></ul> | |
</div> | |
</div> | |
<script src="/vendors.js"></script> | |
<script src="/application.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.get('/greet').then(addNames); | |
$('#name').on('change', function(event){ | |
var name = event.target.value; | |
$('#hello').text('Hello, ' + name + '!'); | |
addNames([name]); | |
$.ajax ({ | |
url: '/greet', | |
type: 'POST', | |
data: JSON.stringify({name: name}), | |
contentType: 'application/json; charset=utf-8' | |
}); | |
}); | |
function addNames(names){ | |
names.forEach(function(name){ | |
$('#greeted').append($('<li>').text(name)); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"rupert": "0.3.x", | |
"rupert-plugin-bootstrap": "^0.1.1", | |
"rupert-plugin-mongo": "0.0.4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var greeted = []; | |
module.exports = function (app, config) { | |
app.get('/greet', function(request, response, next){ | |
app.mongo.db.collection('greeted', function(err, collection){ | |
if(err){ return next(err); } | |
collection.find({}, {}, function(err, cursor){ | |
if(err){ return next(err); } | |
cursor.toArray(function(err, docs){ | |
if(err){ return next(err); } | |
return response.status(200).send(docs.map(function(doc){ | |
return doc.name; | |
})); | |
}); | |
}); | |
}); | |
}); | |
app.post('/greet', function(request, response, next){ | |
app.mongo.db.collection('greeted', function(err, collection){ | |
if(err){ return next(err); } | |
collection.insert({name: request.body.name}, function(err){ | |
if(err){ return next(err); } | |
return response.status(201).send({ok: 1}); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment