Created
August 20, 2012 14:53
-
-
Save kyleondata/3404916 to your computer and use it in GitHub Desktop.
Sinatra 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
require 'sinatra' | |
require 'json' | |
get '/api/hello' do | |
# Return this message in JSON format. | |
content_type :json | |
{ :message => 'Hello World!' }.to_json | |
end |
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
# Add an module you want to run here | |
require './ApiHandler.rb' | |
require './FrontEndHandler.rb' | |
# Then run the app in sinatra. | |
run Sinatra::Application |
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
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 |
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
<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