# Sinatra
[![Gem Version](https://badge.fury.io/rb/sinatra.svg)](http://badge.fury.io/rb/sinatra)
[![Build Status](https://secure.travis-ci.org/sinatra/sinatra.svg)](https://travis-ci.org/sinatra/sinatra)
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=sinatra&package-manager=bundler&version-scheme=semver)
Sinatra is a [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for
quickly creating web applications in Ruby with minimal effort:
```ruby
# myapp.rb
require 'sinatra'
get '/' do
'Hello world!'
end
```
Install the gem:
```shell
gem install sinatra
```
And run with:
```shell
ruby myapp.rb
```
View at: [http://localhost:4567](http://localhost:4567)
The code you changed will not take effect until you restart the server.
Please restart the server every time you change or use
[sinatra/reloader](http://www.sinatrarb.com/contrib/reloader).
It is recommended to also run `gem install puma`, which Sinatra will
pick up if available.
## Table of Contents
* [Sinatra](#sinatra)
* [Table of Contents](#table-of-contents)
* [Routes](#routes)
* [Conditions](#conditions)
* [Return Values](#return-values)
* [Custom Route Matchers](#custom-route-matchers)
* [Static Files](#static-files)
* [Views / Templates](#views--templates)
* [Literal Templates](#literal-templates)
* [Available Template Languages](#available-template-languages)
* [Haml Templates](#haml-templates)
* [Erb Templates](#erb-templates)
* [Builder Templates](#builder-templates)
* [Nokogiri Templates](#nokogiri-templates)
* [Sass Templates](#sass-templates)
* [SCSS Templates](#scss-templates)
* [Less Templates](#less-templates)
* [Liquid Templates](#liquid-templates)
* [Markdown Templates](#markdown-templates)
* [Textile Templates](#textile-templates)
* [RDoc Templates](#rdoc-templates)
* [AsciiDoc Templates](#asciidoc-templates)
* [Radius Templates](#radius-templates)
* [Markaby Templates](#markaby-templates)
* [RABL Templates](#rabl-templates)
* [Slim Templates](#slim-templates)
* [Creole Templates](#creole-templates)
* [MediaWiki Templates](#mediawiki-templates)
* [CoffeeScript Templates](#coffeescript-templates)
* [Stylus Templates](#stylus-templates)
* [Yajl Templates](#yajl-templates)
* [WLang Templates](#wlang-templates)
* [Accessing Variables in Templates](#accessing-variables-in-templates)
* [Templates with `yield` and nested layouts](#templates-with-yield-and-nested-layouts)
* [Inline Templates](#inline-templates)
* [Named Templates](#named-templates)
* [Associating File Extensions](#associating-file-extensions)
* [Adding Your Own Template Engine](#adding-your-own-template-engine)
* [Using Custom Logic for Template Lookup](#using-custom-logic-for-template-lookup)
* [Filters](#filters)
* [Helpers](#helpers)
* [Using Sessions](#using-sessions)
* [Session Secret Security](#session-secret-security)
* [Session Config](#session-config)
* [Choosing Your Own Session Middleware](#choosing-your-own-session-middleware)
* [Halting](#halting)
* [Passing](#passing)
* [Triggering Another Route](#triggering-another-route)
* [Setting Body, Status Code and Headers](#setting-body-status-code-and-headers)
* [Streaming Responses](#streaming-responses)
* [Logging](#logging)
* [Mime Types](#mime-types)
* [Generating URLs](#generating-urls)
* [Browser Redirect](#browser-redirect)
* [Cache Control](#cache-control)
* [Sending Files](#sending-files)
* [Accessing the Request Object](#accessing-the-request-object)
* [Attachments](#attachments)
* [Dealing with Date and Time](#dealing-with-date-and-time)
* [Looking Up Template Files](#looking-up-template-files)
* [Configuration](#configuration)
* [Configuring attack protection](#configuring-attack-protection)
* [Available Settings](#available-settings)
* [Environments](#environments)
* [Error Handling](#error-handling)
* [Not Found](#not-found)
* [Error](#error)
* [Rack Middleware](#rack-middleware)
* [Testing](#testing)
* [Sinatra::Base - Middleware, Libraries, and Modular Apps](#sinatrabase---middleware-libraries-and-modular-apps)
* [Modular vs. Classic Style](#modular-vs-classic-style)
* [Serving a Modular Application](#serving-a-modular-application)
* [Using a Classic Style Application with a config.ru](#using-a-classic-style-application-with-a-configru)
* [When to use a config.ru?](#when-to-use-a-configru)
* [Using Sinatra as Middleware](#using-sinatra-as-middleware)
* [Dynamic Application Creation](#dynamic-application-creation)
* [Scopes and Binding](#scopes-and-binding)
* [Application/Class Scope](#applicationclass-scope)
* [Request/Instance Scope](#requestinstance-scope)
* [Delegation Scope](#delegation-scope)
* [Command Line](#command-line)
* [Multi-threading](#multi-threading)
* [Requirement](#requirement)
* [The Bleeding Edge](#the-bleeding-edge)
* [With Bundler](#with-bundler)
* [Versioning](#versioning)
* [Further Reading](#further-reading)
## Routes
In Sinatra, a route is an HTTP method paired with a URL-matching pattern.
Each route is associated with a block:
```ruby
get '/' do
.. show something ..
end
post '/' do
.. create something ..
end
put '/' do
.. replace something ..
end
patch '/' do
.. modify something ..
end
delete '/' do
.. annihilate something ..
end
options '/' do
.. appease something ..
end
link '/' do
.. affiliate something ..
end
unlink '/' do
.. separate something ..
end
```
Routes are matched in the order they are defined. The first route that
matches the request is invoked.
Routes with trailing slashes are different from the ones without:
```ruby
get '/foo' do
# Does not match "GET /foo/"
end
```
Route patterns may include named parameters, accessible via the
`params` hash:
```ruby
get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params['name'] is 'foo' or 'bar'
"Hello #{params['name']}!"
end
```
You can also access named parameters via block parameters:
```ruby
get '/hello/:name' do |n|
# matches "GET /hello/foo" and "GET /hello/bar"
# params['name'] is 'foo' or 'bar'
# n stores params['name']
"Hello #{n}!"
end
```
Route patterns may also include splat (or wildcard) parameters, accessible
via the `params['splat']` array:
```ruby
get '/say/*/to/*' do
# matches /say/hello/to/world
params['splat'] # => ["hello", "world"]
end
get '/download/*.*' do
# matches /download/path/to/file.xml
params['splat'] # => ["path/to/file", "xml"]
end
```
Or with block parameters:
```ruby
get '/download/*.*' do |path, ext|
[path, ext] # => ["path/to/file", "xml"]
end
```
Route matching with Regular Expressions:
```ruby
get /\/hello\/([\w]+)/ do
"Hello, #{params['captures'].first}!"
end
```
Or with a block parameter:
```ruby
get %r{/hello/([\w]+)} do |c|
# Matches "GET /meta/hello/world", "GET /hello/world/1234" etc.
"Hello, #{c}!"
end
```
Route patterns may have optional parameters:
```ruby
get '/posts/:format?' do
# matches "GET /posts/" and any extension "GET /posts/json", "GET /posts/xml" etc
end
```
Routes may also utilize query parameters:
```ruby
get '/posts' do
# matches "GET /posts?title=foo&author=bar"
title = params['title']
author = params['author']
# uses title and author varia
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
最新版windows logstash-8.3.1-windows-x86_64.zip (12460个子文件)
ChangeLog-0.0.0 810B
bundle-config.1 21KB
bundle-install.1 18KB
bundle-update.1 14KB
bundle-exec.1 7KB
bundle-gem.1 5KB
rake.1 4KB
rake.1 4KB
bundle.1 4KB
bundle-outdated.1 3KB
bundle-cache.1 3KB
bundle-lock.1 3KB
mustache.1 3KB
tt.1 2KB
bundle-add.1 2KB
bundle-pristine.1 2KB
bundle-binstubs.1 2KB
bundle-platform.1 1KB
bundle-doctor.1 1KB
bundle-viz.1 1KB
bundle-init.1 1KB
bundle-check.1 961B
bundle-list.1 960B
bundle-remove.1 844B
bundle-inject.1 733B
bundle-show.1 685B
bundle-clean.1 599B
bundle-open.1 576B
bundle-info.1 444B
ChangeLog-0.0.1 286B
urls.10K 686KB
ChangeLog-0.0.2 2KB
ChangeLog-0.0.3 4KB
ChangeLog-0.0.4 2KB
gemfile.5 22KB
mustache.5 8KB
libjffi-1.2.a 165KB
libjffi-1.2.a 143KB
configure.ac 4KB
jmxremote.access 4KB
ADDITIONAL_LICENSE_INFO 2KB
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
ADDITIONAL_LICENSE_INFO 49B
共 12460 条
- 1
- 2
- 3
- 4
- 5
- 6
- 125
资源评论
h735004790
- 粉丝: 33
- 资源: 1278
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功