Dancer--introduction小议❹

Dancer--introduction小议❹,第1张

概述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 ($fi

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; 

总结

以上是内存溢出为你收集整理的Dancer--introduction小议❹全部内容,希望文章能够帮你解决Dancer--introduction小议❹所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1283446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存