用newlisp开发网站二 静态文件路由

来源:互联网 发布:淘宝私密优惠券地址 编辑:程序博客网 时间:2024/06/08 00:45

URL路由到本地html文件

当我输入http://localhost:8080/foo时,会打开foo目录下的html文件。原因是在

config.lsp文件中有如下配置:

(constant 'STATIC_TRANSFORMATIONS '((string DOCUMENT_ROOT "/" _ "/index.html")(string VIEWS_PATH "/" _)(string VIEWS_PATH "/" _ VIEW_EXTENSION)))

注意这里的_ 符号,这里自动匹配了foo,因此第一条规则找到了index.html文件。其实这个特性对于熟练掌握Nginx的人来讲诱惑不大。

如果匹配不到本地目录,则自动使用views目录下的文件,比如:

http://localhost:8080/dragonfly_welcome
就转向了views/dragonfly_welcom.html文件。

dragonfly支持三种路由方式,本节描述的是静态路由,路由到本地静态文件。

当参考http://dragonfly.neocortex.io/dragonfly_routes的下面一节

Templates with Routes.Static

时,要注意,第一个匹配规则

(constant 'STATIC_TRIGGER_EXTENSIONS '(".html"))

意味这你要写全路径,比如:http://localhost:8080/views/dragonfly_welcome.html

这样规则生效,就能访问到正确路径。

如果写成这样:http://localhost:8080/dragonfly_welcome

那么第一条规则匹配失败,则被上面的STATIC_TRANSFORMATIONS的第三条规则匹配到,也能访问。


正则表达式解析

通过跟踪newlisp-redirection.lsp的代码,发现正则表达式原来是用来提取HTTP请求的URL的最后一段,如:

> (regex {\w+\s+/([^\s]+)} "GET /dragonfly_welcome HTTP/1.1^M")("GET /dragonfly_welcome" 0 22 "dragonfly_welcome" 5 17)

然后添加上扩展名.html,跳转到views目录的dragonfly_welcome.html文件。

之所以能跳转到views目录,是因为在dragonfly-framework/config.json文件中有这一行配置:

(context 'Dragonfly);===============================================================================; Default locations;===============================================================================; location of views (for use with 'display-view' function)(constant 'VIEWS_PATH (string DOCUMENT_ROOT "/views")); location of partials (for use with 'display-partial' function)(constant 'PARTIALS_PATH (string DOCUMENT_ROOT "/views/partials")); setting a default view (sans file extension)(constant 'DEFAULT_VIEW "welcome"); used by 'display-view' to save you keystrokes and by the static routing.(constant 'VIEW_EXTENSION ".html")


所以结论是,如果有html文件,都放到views目录下。