Last active
August 29, 2015 14:22
-
-
Save DavidSouther/501ae1368bee62025a75 to your computer and use it in GitHub Desktop.
Rupert API 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-api-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>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> |
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" | |
} | |
} |
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){ | |
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