# Sinatra
[![Gem Version](https://badge.fury.io/rb/sinatra.svg)](https://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 vari
没有合适的资源?快使用搜索试试~ 我知道了~
arachni-1.6.1.3-0.6.1.1-linux-x86-64
需积分: 0 0 下载量 23 浏览量
2024-02-06
10:32:26
上传
评论
收藏 261.55MB GZ 举报
温馨提示
共2000个文件
h:1164个
html:249个
md:219个
web漏洞扫描工具,给下载费劲的朋友
资源推荐
资源详情
资源评论
收起资源包目录
arachni-1.6.1.3-0.6.1.1-linux-x86-64 (2000个子文件)
pg_connection.c 130KB
xml_node.c 57KB
parser.c 56KB
ox.c 55KB
generator.c 51KB
sax.c 45KB
pg_result.c 45KB
dump.c 33KB
crypt_blowfish.c 32KB
pg.c 29KB
pg_text_decoder.c 27KB
parse.c 27KB
cparse.c 24KB
pg_text_encoder.c 24KB
obj_load.c 23KB
builder.c 23KB
database.c 21KB
pg_record_coder.c 18KB
pg_copy_coder.c 18KB
gumbo.c 18KB
xml_document.c 17KB
pg_coder.c 16KB
xml_reader.c 15KB
wrapper.c 15KB
gen_load.c 14KB
pg_tuple.c 13KB
xml_node_set.c 11KB
statement.c 11KB
pg_type_map_by_oid.c 11KB
xml_xpath_context.c 10KB
pg_type_map_in_ruby.c 10KB
pg_type_map_by_column.c 10KB
xslt_stylesheet.c 10KB
sax_hint.c 9KB
aggregator.c 9KB
nokogiri.c 8KB
xml_sax_parser.c 8KB
pg_type_map_by_mri_type.c 8KB
pg_binary_decoder.c 8KB
pg_type_map_by_class.c 7KB
xml_schema.c 7KB
html4_element_description.c 6KB
sax_as.c 6KB
xml_sax_parser_context.c 6KB
sax_buf.c 6KB
hash_load.c 5KB
sqlite3.c 5KB
html4_document.c 5KB
pg_type_map.c 5KB
pg_binary_encoder.c 5KB
pg_util.c 5KB
cache.c 5KB
backup.c 4KB
xml_relax_ng.c 4KB
xml_dtd.c 4KB
crypt_gensalt.c 4KB
xml_sax_push_parser.c 4KB
xml_namespace.c 3KB
pg_type_map_all_strings.c 3KB
bcrypt_ext.c 3KB
base64.c 3KB
html4_sax_parser_context.c 3KB
exception.c 3KB
rb_concurrent.c 3KB
libxml2_backwards_compat.c 3KB
xml_entity_decl.c 3KB
xml_element_content.c 3KB
pg_errors.c 2KB
atomic_reference.c 2KB
html4_sax_push_parser.c 2KB
xml_encoding_handler.c 2KB
xml_syntax_error.c 2KB
xml_attr.c 2KB
cache8.c 2KB
atomic_fixnum.c 2KB
special.c 2KB
xml_comment.c 2KB
xml_attribute_decl.c 1KB
xml_element_decl.c 1KB
atomic_boolean.c 1KB
xml_cdata.c 1KB
xml_processing_instruction.c 1KB
test_global_handlers.c 1KB
xml_entity_reference.c 1KB
crypt.c 1KB
html4_entity_lookup.c 1KB
xml_text.c 961B
xml_document_fragment.c 938B
err.c 857B
websocket_mask.c 767B
gvl_wrappers.c 639B
main.c 103B
main.c 103B
a.c 65B
b.c 65B
a.c 65B
b.c 65B
application-60b0c9700d234fe687970343a84d77a4d0991781908521792fdb1c8f6d534a8d.css 154KB
bootstrap_and_overrides-d23fd90c927e7917e35095a7428e1bde142cccb67890b8be60c67ebc4bacd1fd.css 120KB
bootstrap.min.css 98KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
adcsdnmin
- 粉丝: 2
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功