Skip to content

Instantly share code, notes, and snippets.

@kyleondata
Created August 20, 2012 14:53
Show Gist options
  • Save kyleondata/3404916 to your computer and use it in GitHub Desktop.
Save kyleondata/3404916 to your computer and use it in GitHub Desktop.
Sinatra example
require 'sinatra'
require 'json'
get '/api/hello' do
# Return this message in JSON format.
content_type :json
{ :message => 'Hello World!' }.to_json
end
# Add an module you want to run here
require './ApiHandler.rb'
require './FrontEndHandler.rb'
# Then run the app in sinatra.
run Sinatra::Application
require 'sinatra'
get '/' do
# Anything in the public folder gets served up
# automatically by Sinatra so this is where I put
# HTML, CSS, and image files.
# This acts as a URL router and just takes the HTML
# and returns it.
File.read(File.join('public', 'index.html'))
end
<html>
<head>
<script src='/jquery-1.7.2.min.js'></script>
<script>
$(document).ready(function(){
$.ajax({
url: 'http://localhost:8080/api/hello',
dataType: 'json',
}).done(function(data){
$('body').html(data.message);
});
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment