Skip to content

Instantly share code, notes, and snippets.

@mmalecki
Created February 5, 2012 18:14
Show Gist options
  • Select an option

  • Save mmalecki/1746970 to your computer and use it in GitHub Desktop.

Select an option

Save mmalecki/1746970 to your computer and use it in GitHub Desktop.
var util = require('util'),
Plates = require('plates'),
flatiron = require('../'),
app = flatiron.app;
app.use(flatiron.plugins.http);
var sandwiches = {
bacon: 24,
burger: 42
};
app.router.get('/', function () {
this.res.writeHead(200, { 'Content-Type': 'text/plain' });
this.res.end('Hello world!\n');
});
app.router.post('/', function () {
this.res.writeHead(200, { 'Content-Type': 'text/plain' });
this.res.write('Hey, you posted some cool data!\n');
this.res.end(util.inspect(this.req.body, true, 2, true) + '\n');
});
app.router.get('/sandwich/:type', function (type) {
if (sandwiches[type]) {
this.res.writeHead(200, { 'Content-Type': 'text/plain' });
sandwiches[type]--;
this.res.end('Serving ' + type + ' sandwich!\n');
}
else {
this.res.writeHead(404, { 'Content-Type': 'text/plain' });
this.res.end('No such sandwich, sorry!\n');
}
});
app.router.get('/sandwich', function () {
this.res.writeHead(200, { 'Content-Type': 'text/html' });
var html = [
'<p>Burger sandwiches left <div id="burger"></div>',
'<p>Bacon sandwiches left <div id="bacon"></div>'
].join('');
this.res.end(Plates.bind(html, sandwiches));
});
app.start(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment