Skip to content

Instantly share code, notes, and snippets.

@DavidSouther
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidSouther/501ae1368bee62025a75 to your computer and use it in GitHub Desktop.
Save DavidSouther/501ae1368bee62025a75 to your computer and use it in GitHub Desktop.
Rupert API Example
var config = {
name: "rupert-api-example",
stassets: {
root: './src/client'
},
server: {
root: './src/server'
}
};
require('rupert')(config).start();
<!DOCTYPE html5>
<html lang="en">
<head>
<title>Basic Demo</title>
<link rel="stylesheet" href="/all.css"/>
</head>
<body>
<h1 class="title">Rupert Basic Demo</h1>
<div>
<p>What's your name?</p>
<input id="name" />
<p id="hello"></p>
<h2>I have greeted</h2>
<ul id="greeted"></ul>
</div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="/application.js"></script>
</body>
</html>
$.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));
});
}
{
"dependencies": {
"rupert": "0.3.x"
}
}
var greeted = [];
module.exports = function (app, config) {
app.get('/greet', function(request, response){
response.status(200).send(greeted);
});
app.post('/greet', function(request, response){
greeted.push(request.body.name);
response.status(201).send({ok: 1});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment