加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Dancer--introduction小议?

发布时间:2020-12-16 00:21:06 所属栏目:大数据 来源:网络整理
导读: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)?=?@{?$p

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;?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读