目录
这份教程旨在告诉你如何安装并通过elnode提供web服务.
Elnode是一款在Emacs上的类似node.js的webserver工具. 它使得Emacs能够提供web服务
安装Enode
可以共过package来安装Elnode,地址为 Marmalade.
关于package仓库的更多知识参见Emacs Wiki. 简单来讲,只需将以下代码添加到你的~/.emacs或~/.emacs.d/init.el文件中
(add-to-list
'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/"))
然后执行 M-x list-packages
从列表中找到Elnode,然后按i或RET来安装
若你不想使用package来安装,可以手工将”elnode.el”放入`load-path’中,然后执行 (require 'elnode)
Hello World!
现在我们安装好了Elnode,之后就可以用它来提供web服务了. 下面我们展示一个例子,用页面显示”Hello World”
使用 C-x C-f my-elnode-hello-world.el 创建一个新文件. 然后输入如下代码
(defun my-elnode-hello-world-handler (httpcon)
(elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
(elnode-http-return
httpcon
"<html><body><h1>Hello World</h1></body></html>"))
(elnode-start 'my-elnode-hello-world-handler :port 8028 :host "localhost")
使用 M-x eval-buffer 运行该段代码
现在用浏览器打开http://localhost:8028 就能看到”Hello World!”了
发布文件
Elnode提供了内置的web服务功能,可以用于将计算机中某个目录中那个的文件发布出来. 默认情况下加载Elnode即会打开内置的web服务功能(当然这是可配置的)
默认的web服务
默认情况下,Elnode将`~/public_html’中的文件发布出来,默认的访问地址为http://localhost:8000
当然我们也可以更改web服务器的根目录地址,方法是 M-x customize-variable RET elnode-webserver-docroot RET
启动新web服务器
下面将介绍如何再启动一个新的服务器.
- 创建新的服务器根目录
mkdir ~/myspecialdocroot
放一个html文件到新创建的服务器根目录
cat <<EOF > ~/myspecialdocroot/saybum.html <html> <h1>BUM!</h1> </html>使用
C-x C-f my-elnode-webserver.el创建新文件输入如下代码
(defconst my-elnode-webserver-handler
(elnode-webserver-handler-maker "~/myspecialdocroot"))
(elnode-start my-elnode-webserver-handler :port 8001 :host "localhost")
使用
M-x eval-buffer执行这段代码现在可以通过http://localhost:8001/saybum.html 访问新创建的网页
通过http://localhost:8001 可以访问`~/myspecialdocroot’中的index页面
关闭web服务
通过 M-x elnode-stop RET 8028 RET 和 M-x elnode-stop RET 8001 RET 可以停止刚才创建的两个web服务器
为内置服务器添加绑定(Add a binding to the builtin server)
Instead of starting new servers all the time we can add bindings to
the standard Elnode server. Why would we do this? I think using a
separate server for developing something initially is a good idea, but
then you either have something you want to package up as it’s own
server (a wiki engine you’ve developed and want to give to other
people, for example) or you have something you want to make available
in your own default server. Of course, it’s always a judgement, the
way URLs work mean that you can pretty much always make any service
available on it’s own server or under a URL on another one.
下面让我们将上面的Hello World例子中的处理器绑定到默认服务器上(这样我们可以通过8000端口访问到Hello World页面)
使用
C-x b my-elnode-hello-world.el回到Hello World例子中的源文件删除
elnode-start这一行,并添加下面代码
(add-to-list 'elnode-hostpath-default-table '("/helloworld/" . my-elnode-hello-world-handler))
整个代码变成如下所示
(defun my-elnode-hello-world-handler (httpcon)
(elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
(elnode-http-return
httpcon
"<html><body><h1>Hello World</h1></body></html>"))
(add-to-list 'elnode-hostpath-default-table '("/helloworld/" . my-elnode-hello-world-handler))
使用
M-x eval-buffer执行这段代码为了验证默认服务器依然有效,访问http://localhost:8000/. 可以看到依然会显示`~/public_html’中的index页面
使用
C-h v elnode-hostpath-default-table查看变量`elnode-hostpath-default-table’. 其值应该如下所示
(("/helloworld/" . my-elnode-hello-world-handler)
("[^/]+/.*" . elnode-webserver))
- 通过配置变量`elnode-hostpath-default-table’的值可以添加更多的web服务. 但这种手工修改的动作必须在Emacs启动时完成.
一个更高级的例子–如何发布一个buffer
目前为止的所有例子都很简单. 希望这种简单性能激发你的兴趣.
下面让我们来点复杂的–创建一个web编辑器
这个例子会随着教程而不断更新. 第一个版本相对简单,但希望它能够引发你的兴趣.
使用
C-x C-f my-elnode-editor.el创建新文件.添加如下代码
(defvar my-elnode-editor-buffer (get-buffer-create "*my-elnode-editor-buffer*"))
(defun my-elnode-editor-handler (httpcon)
(elnode-http-start httpcon 200 '("Content-Type" . "text/plain"))
(elnode-http-return
httpcon
(with-current-buffer my-elnode-editor-buffer
(buffer-substring-no-properties (point-min) (point-max)))))
使用
M-x eval-buffer执行这段代码. 现在在`*my-elnode-editor-buffer*’buffer中输入内容,这些内容就会作为网页的内容显示出来开启服务
M-x elnode-start my-elnode-editor-handler 8002 localhost访问http://localhost:8002 将会看到`*my-elnode-editor-buffer*’中的内容
更新buffer内容后,刷新浏览器会看到浏览器中显示的内容也做出了相应的改变。
至此,我们发布了一个buffer。 但若希望让访问者能够更新buffer内容,又该如何操作呢?
创建另一个处理器来处理访问者的更新请求。
添加下面这段代码到`my-elnode-editor.el’中
(defun my-elnode-editor-update-handler (httpcon)
(let ((change-text (elnode-http-param httpcon "change")))
(with-current-buffer my-elnode-editor-buffer
(goto-char (point-max))
(if (stringp change-text)
(insert change-text))))
(elnode-http-start httpcon 302 '("Location" . "/"))
(elnode-http-return httpcon))
将两个处理器映射到一起。
假设我们将第一个处理器映射到/路径,第二个处理器映射到/update/则需要添加如下代码到`my-elnode-editor.el’中
(defconst my-elnode-editor-urls
`(("^/$" . my-elnode-editor-handler)
("^/update/.*$" . my-elnode-editor-update-handler)))
添加处理器以分发URL
将下面代码加入`my-elnode-editor.el’中
(defun my-elnode-editor-dispatcher-handler (httpcon)
(elnode-dispatcher httpcon my-elnode-editor-urls))
**What is a dispatcher?** - a dispatcher is a handler that take a list of URL pattern mappings and works out, by reading the data from the HTTP connection, what handler should be invoked for what request.
重启服务
M-x elnode-stop 8002 M-x elnode-start my-elnode-editor-dispatcher-handler 8002 localhost访问http://localhost:8002 可以看到buffer内容. 访问http://localhost:8002/update/?change=%0dlah+dee+dah%0d 可以看到buffer内容发生了改变
高级案例 - 围绕服务创建一个web app
现在让我们添加一些静态文件,并使得可以通过客户端的Javascript来调用Elnode处理器.
首先我们来給`/text’路径绑定上处理器
(defconst my-elnode-editor-webserver-handler
(elnode-webserver-handler-maker "~/my-directory")
"The webserver handler.")
(defconst my-elnode-editor-urls
'(("^/text/$" . my-elnode-editor-handler)
("^/update/.*$" . my-elnode-editor-update-handler)
("^/[^/]+/.*$" . my-elnode-editor-webserver-handler)))
很明显`~/my-directory’就是你存放HTML和Javascript文件的地方.
然后创建一个名为`my-elnode-editor.html’的HTML文件,内容为
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"
language="Javascript">
</script>
<script src="my-elnode-editor.js" language="Javascript">
</script>
</head>
<body>
<textarea id="text" cols="60" rows="10">
</textarea>
</body>
</html>
最后创建一个名为`my-elnode-editor.js’的文件,文件内容为:
var my_elnode_editor = (function ()
var self = {
/** Get the text from Emacs.
*/
get_text: function () {
$.ajax("/text/", {
dataType: "text",
success: function (data, textStatus, jqXHR) {
$("#text").text(data);
}
});
}
};
return self;
})();
$(document).ready(
function () {
my_elnode_editor.get_text();
}
);
这段javascript在页面加载时,通过Ajax从Elnode获取buffer内容,并将获取的内容填入textarea中
你甚至都不需要重启Elnode处理器,因为它已经指向了哪些分发处理器了.
只需要通过 M-x eval-buffer 重新执行一下URL映射即可.
现在你访问http://localhost:8002/my-elnode-editor.html 就能看到一个包含文本框的网页,文本框中的内容就是buffer中的内容
1082

被折叠的 条评论
为什么被折叠?



