Dancer--introduction小议❹
STATIC FILE FROM A ROUTE HANDLER----->路由处理中的静态文件
It's possible for a route handler to send a static file, as follows:
路由处理也可以发送一个静态文件,如下:
get '/download/*' => sub {
my $params = shift;
my ($file) = @{ $params->{splat} };
send_file $file;
};
Or even if you want your index page to be a plain old index.html file, just do:
或者更甚者你想让index页面变成一个普通的index.html文件,需如下操作:
get '/' => sub {
send_file '/index.html'
};
SETTINGS------>设置
It's possible to change quite every parameter of the application via the settings mechanism.
你可以通过设置机制来完全的改变应用中的每一个参数。
A setting is key/value pair assigned by the keyword set:
一个设置是由关键字set分配的一个key/value对。
set setting_name => 'setting_value';
More usefully, settings can be defined in a YAML configuration file. Environment-specific settings can also be defined in environment-specific files (for instance, you don't want auto_reload in production, and might want extra logging in development). See the cookbook for examples.
在YAML配置文件中定义那些设置会更加的有效,指定的环境设置可以定义在指定的环境文件中(如:你不希望在生产环境中使用auto_reload功能,也许会在开发环境中使用额外的日志功能),如果想了解更多,请阅读cookbook中的例子。
See Dancer::Config for complete details about supported settings.
如果想了解更多的关于dancer支持的设置请参考Dancer::Config文档。
SERIALIZERS------>序列化
When writing a webservice, data serialization/deserialization is a common issue to deal with. Dancer can automatically handle that for you, via a serializer.
当写web服务时,通常需要处理的一个问题就是数据的序列化和反序列化。但是,dancer会自动的通过serializer功能来处理上述问题。
When setting up a serializer, a new behaviour is authorized for any route handler you define: any response that is a reference will be rendered as a serialized string, via the current serializer.
当设置一个"serializer"时,你所定义的每一个路由处理都会添加一个新的行为,涉及到的每一个响应都会通过当前的"serializer"将每一个响应以serialized 串的形式来响应。
Here is an example of a route handler that will return a HashRef
下面是一个路由处理返回一个hashref的例子:
use Dancer;
set serializer => 'JSON';
get '/user/:id/' => sub {
{ foo => 42,
number => 100234,
list => [qw(one two three)],
}
};
As soon as the content is a reference - and a serializer is set, which is not the case by default - Dancer renders the response via the current serializer.
一旦有内容触发了serializer那么serializer就会被设置,这并不是默认的操作,因为需要前面的设置,dancer会通过当前的serializer发送响应。
Hence, with the JSON serializer set, the route handler above would result in a content like the following:
因此,当设置了JSON-serializer,那么上面的路由处理会返回下面的结果:
{'number':100234,'foo':42,'list':['one','two','three']}
The following serializers are available, be aware they dynamically depend on Perl modules you may not have on your system.
虽然下面的序列化方法是可用的,但是你需要清楚的是,她们都是动态加载的依赖perl模块的,有些模块也许你还没有安装到系统中。
JSON
requires JSON
YAML
requires YAML
XML
requires XML::Simple
Mutable
will try to find the appropriate serializer using the Content-Type and Accept-type header of the request.
Mutable可以通过在请求头中使用Content-Type and Accept-type来寻找合适的可序列化(就是说,看什么序列化能被正确使用)。
EXAMPLE------>范例
This is a possible webapp created with Dancer:
下面是使用dancer编写的一个webapp。
#!/usr/bin/perl
# make this script a webapp
use Dancer;
# declare routes/actions
get '/' => sub {
'Hello World';
};
get '/hello/:name' => sub {
'Hello '.param('name');
};
# run the webserver
Dancer->dance;
转载于:https://blog.51cto.com/perfect/931526
Dancer--introduction小议❹相关推荐
- dancer cookbook 小议3
毁坏一个会话 When you're done with your session, you can destroy it: session->destroy Sessions and logg ...
- Dancer--introduction小议❶
Dancer::Introduction - A gentle introduction to Dancer DESCRIPTION--->简介 Dancer is a free and ope ...
- “HOW CAN WE KNOW THE DANCER FROM THE DANCE?”: COGNITIVE POETICS AND WILLIAM BUTLER【翻译】
Pagel, Amber Noelle. "How Can We Know the Dancer from the Dance?": Cognitive Poetics and W ...
- Blender 3.0基础入门学习教程 Introduction to Blender 3.0
成为Blender通才,通过这个基于项目的循序渐进课程学习所有主题的基础知识. 你会学到什么 教程获取:Blender 3.0基础入门学习教程 Introduction to Blender 3.0- ...
- 网络增强现实开发简介 Introduction to Web AR development
搭配webXR.mindAR.three.js和tensorflow.js 你会学到: 获得构建不同类型的网络增强现实应用程序的实践经验,包括图像效果.人脸效果和世界效果 获得关于增强现实如何在网络浏 ...
- ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush
ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush ...
- 视频色彩校正简介 Introduction to Video Color Correction
视频色彩校正简介 Introduction to Video Color Correction 视频色彩校正简介 Introduction to Video Color Correction MP4 ...
- [转]Introduction of iSCSI Target in Windows Server 2012
Introduction of iSCSI Target in Windows Server 2012 源地址:http://blogs.technet.com/b/filecab/archive/2 ...
- MS UI Automation Introduction
MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...
- 音频(3):iPod Library Access Programming Guide:Introduction
Next Introduction 介绍 iPod库访问(iPod Library Access)让应用程序可以播放用户的歌曲.有声书.和播客.这个API设计使得基本播放变得非常简单,同时也支持高级的 ...
最新文章
- ifstream java_C ifstream将读取一些值然后停止
- Fastboot的安装与使用
- 解析html文档的java库及范例
- 导出目录结构_Selenium Webdriver 3.X源码分析之核心目录结构
- 向linux内核版本号添加字符/为何有时会自动添加“+”号
- matlab 大于并且小于,Matlab:将大于(小于)1(-1)的元素转换为1(-1)的序列
- 小猪佩奇python_python画个小猪佩奇
- Membership Inference Attacks Against Recommender Systems论文解读
- windows 安装mongodb
- IBM的人工智能“沃森”首次确诊罕见白血病,只用了10分钟!
- 巴克莱银行实现敏捷及DevOps与本地文化的融合
- 《锋利的jQuery》笔记 第2章 jQuery选择器
- 计算机制图师岗位技能要求,机械制图员
- 博科光纤交换机默认密码更改
- 单片机程序框架设计与实现
- Word操作之图表目录自动生成
- 中华人民共和国民法典
- 第二章 马原刷题(1)
- WordPress的.htaccess优化技巧是什么
- 华硕路由 ac ax_【路由器】华硕网络开启802.11ax电竞生态