# Sinatra
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)
It is recommended to also run `gem install thin`, 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)
* [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)
* [Roll Your Own](#roll-your-own)
* [Install Globally](#install-globally)
* [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.
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 /\A\/hello\/([\w]+)\z/ 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 variables; query is optional to the /posts route
end
```
By the way, unless you disable the path traversal attack protection (see below),
the request path might be modified before matching against your routes.
## Conditions
Routes may include a variety of matching conditions, such as the user agent:
```ruby
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
"You're using Songbird version #{params['agent'][0]}"
end
get '/foo' do
# Matches non-songbird browsers
end
```
Other available conditions are `host_name` and `provides`:
```ruby
get '/', :host_name => /^admin\./ do
"Admin Area, Access denied!"
end
get '/', :provides => 'html' do
haml :index
end
get '/', :provides => ['rss', 'atom', 'xml'] do
builder :feed
end
```
`provides` searches the request's Accept header.
You can easily define your own conditions:
```ruby
set(:probability) { |value| condition {
没有合适的资源?快使用搜索试试~ 我知道了~
logstash-7.1.1.zip
共2000个文件
rb:7592个
json:1818个
md:403个
需积分: 10 19 下载量 154 浏览量
2019-09-06
00:49:22
上传
评论
收藏 170.75MB ZIP 举报
温馨提示
Logstash是一个开源的日志收集管理工具。... logstash作为一个数据管道中间件,支持对各种类型数据的采集与转换,并将数据发送到各种类型的存储库
资源推荐
资源详情
资源评论
收起资源包目录
logstash-7.1.1.zip (2000个子文件)
test.c 88KB
test.c 76KB
http_parser.c 60KB
http_parser.c 57KB
xml_node.c 43KB
http11_parser.c 24KB
ng.c 20KB
xml_document.c 15KB
ruby_http_parser.c 14KB
xml_reader.c 14KB
puma_http11.c 13KB
xml_node_set.c 12KB
hitimes_interval.c 10KB
mini_ssl.c 9KB
xml_sax_parser.c 8KB
xml_xpath_context.c 8KB
xslt_stylesheet.c 7KB
hitimes_stats.c 6KB
html_element_description.c 6KB
xml_sax_parser_context.c 6KB
xml_schema.c 4KB
html_document.c 4KB
nokogiri.c 4KB
xml_dtd.c 4KB
atomic_reference.c 4KB
xml_sax_push_parser.c 4KB
xml_relax_ng.c 4KB
io_buffer.c 3KB
html_sax_parser_context.c 3KB
xml_libxml2_hacks.c 3KB
api.c 3KB
xml_entity_decl.c 3KB
xml_element_content.c 3KB
xml_namespace.c 2KB
xml_attr.c 2KB
html_sax_push_parser.c 2KB
xml_syntax_error.c 2KB
xml_encoding_handler.c 2KB
xml_comment.c 2KB
xml_cdata.c 1KB
xml_attribute_decl.c 1KB
xml_element_decl.c 1KB
xml_processing_instruction.c 1KB
hitimes_instant_osx.c 1KB
xml_io.c 1KB
xml_entity_reference.c 1KB
xml_text.c 1KB
xml_document_fragment.c 1KB
hitimes.c 894B
html_entity_lookup.c 813B
mytest.c 670B
hitimes_instant_clock_gettime.c 610B
hitimes_instant_windows.c 582B
main.c 103B
main.c 103B
main.c 103B
a.c 65B
b.c 65B
a.c 65B
b.c 65B
a.c 65B
b.c 65B
rdoc.css 7KB
common.css 2KB
common.css 136B
app.css 17B
snappy-test.h 17KB
snappy-stubs-internal.h 15KB
http_parser.h 11KB
http_parser.h 11KB
snappy.h 8KB
snappy-internal.h 5KB
snappy-c.h 5KB
snappy-sinksource.h 5KB
nokogiri.h 3KB
hitimes_interval.h 2KB
http11_parser.h 1KB
xml_sax_parser.h 841B
xml_document.h 705B
ext_help.h 650B
hitimes_stats.h 577B
ext_help.h 548B
xml_node_set.h 377B
xml_syntax_error.h 359B
xml_xpath_context.h 311B
xml_node.h 305B
xml_namespace.h 299B
xslt_stylesheet.h 292B
xml_libxml2_hacks.h 258B
xml_io.h 257B
xml_element_content.h 218B
xml_processing_instruction.h 208B
html_element_description.h 202B
html_sax_parser_context.h 197B
xml_sax_parser_context.h 192B
xml_document_fragment.h 189B
xml_entity_reference.h 184B
html_sax_push_parser.h 184B
xml_sax_push_parser.h 180B
xml_attribute_decl.h 176B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
牛仔0314
- 粉丝: 1
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功