Web.js framework for Node.js

我用Node.js的Module功能,写了一个类似于Web.py的Node.js框架,不过也会逐渐脱离模仿Web.py的。
Github在这里https://github.com/iwillwen/web.js
以下是现在版本的一些功能

  • 实现了URL路由功能
  • 实现GET和POST的HTTP请求响应
  • 实现自定义404页面
  • 实现GET和POST响应器出错时的备用错误处理器
  • 引用Mustache模版引擎的方法

一个小DEMO

先把web文件夹复制到你的项目里,然后require(‘./web/web’)就行了。

URL路由映射设置

var qs = require("querystring"),
    web = require("./web/web");

var urls = {
            "test" : "simple.txt",
            "test/([a-zA-Z0-9])" : "test.html",
            "^[0-9]" : "testimage.jpg"
            };

web.run(urls, 8888, "127.0.0.1");
console.log("It's running on http://localhost:8888");

上面这段代码做了一个简单的HTTP服务器和URL路由映射功能(支持正则):

  1. 当浏览http://localhost:8888/的时候就会看到主页
  2. 当浏览http://localhost:8888/test/的时候就会返回simple.txt里面的内容
  3. 当浏览http://localhost:8888/test/+任意字符(如http://localhost:8888/test/foo)的时候就会返回test.html
  4. 当浏览http://localhost:8888/+任意数字的时候就会返回一个名为testimage.jpg的图片

GET和POST的响应器设置

var qs = require("querystring"),
    web = require("./web/web");

var urls = {
            "test" : "simple.txt",
            "test/([a-zA-Z0-9])" : "test.html",
            "^[0-9]" : "testimage.jpg"
            },
    gets = {
            "get" : function (req, res, qs) {
                        var qss = "";
                        for (var key in qs) {
                            qss += key + ":" + qs[key] + "
";
                        }
                        web.send(res, 'Test the GET
' + qss);
                    },
            "a" : function (req, res, qs) {
                        gt;
                    }
            },
    posts = {
            "post" : function (req, res, data) {
                        web.send(res, "POST success.");
                    }
            };w

eb.run(urls, 8888, "127.0.0.1");
web.get(gets);
web.post(posts);

console.log("It's running on http://localhost:8888");

这里定义了两个Object,和URL规则一样,但是不支持正则(出于性能考虑),值需要是一个callback函数,需要传入三个参数:Request、Response、Data(一个包含请求内的数据的Obj,当中的set函数请无视。。)。

自定义404页面

set404方法,只需要传入一个指定404文件的文件名就可以了。

var qs = require("querystring"),
    web = require("./web/web");

var urls = {
            "test" : "simple.txt",
            "test/([a-zA-Z0-9])" : "test.html",
            "^[0-9]" : "testimage.jpg"
            },
    gets = {
            "get" : function (req, res, qs) {
                        var qss = "";
                        for (var key in qs) {
                            qss += key + ":" + qs[key] + "
";
                        }
                        web.send(res, 'Test the GET
' + qss);
                    },
            "a" : function (req, res, qs) {
                        gt;
                    }
            },
    posts = {
            "post" : function (req, res, data) {
                        web.send(res, "POST success.");
                    }
            };

web.run(urls, 8888, "127.0.0.1");
web.get(gets);
web.post(posts);
web.set404("404.html");

console.log("It's running on http://localhost:8888");

GET和POST错误设置

var qs = require("querystring"),
    web = require("./web/web");

var urls = {
            "test/([a-zA-Z0-9])" : "test.html",
            "[0-9]" : "testimage.jpg"
            },
    gets = {
            "get" : function (req, res, qs) {
                        var qss = "";
                        for (var key in qs) {
                            qss += key + ":" + qs[key] + "
";
                        }
                        web.send(res, 'Test the GET
' + qss);
                    },
            "a" : function (req, res, qs) {
                        gt;
                    }
            }
    posts = {
            "post" : function (req, res, data) {
                        web.send(res, "POST success.");
                    }
            },
    erorrs = {
            "get" : function (req, res) {
                        web.send(res, "Get handler has gone wrong");
                    },
            "post" : function (req, res) {
                        web.send(res, "Post handler has gone wrong");
                    }
            };

web.run(urls, 8888, "127.0.0.1");
web.get(gets);
web.post(posts);
web.set404("404.html");
web.erorr(erorrs);

console.log("It's running on http://localhost:8888"); 

还是一样的用法,但是是一个get属性和post属性,传入Request和Response。

嗯,今天先到这里。。以后更新了再补充。。塞哟拉拉

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。