ASP框架

现在服务端的JS挺红火的,如 nodejs 和建于其上的 express框架

 

我也搞一个,就用ASP的后台Jscript 来模拟一个简单的REST框架出来。

 

原理很简单:

 

1、设置IIS的默认页为index.asp

2、设置IIS的404错误处理:跳转到 /index.asp

3、在index.asp里解析Request信息,其中QueryString包含了url,是重点!

4、在解析中,不同的 url 对应到不同的回调函数。(类似 express,但要加上层次特性)

 

我先把用法贴出来,听听大家的意见,好改进,大家觉得有用,我再把实现代码贴出来。

针对接口编程嘛! 

 

<script runat="server" language="javascript" src="core.js"></script>
<script runat="server" language="javascript">



Core.on("/", function(req){
    
    this.charset("utf-8")
    this.layout("global.html")
    
    Core.get("(index)?$", function(){
        this.title = "welcome!"
        this.template("index.html")
    })
    
    
    Core.on("users", function(){
        
        var db = Core.openDB("access://blog.mdb"); 
        
        Core.on("$", function(){
            
            if (req.method == "GET")
            {
                this.users = db.exec("select * from users")
                this.template("users.html")
            }
            
            if (req.method == "POST")
            {
                this.ok = db.exec("insert into users (name, sex) values(?,?)",[req("name"), req("sex")]);
                this.redirect("/users")
            }
            
        })
        Core.on("/:id", function(id){
            
            if (req.method == "GET")
            {
                this.user = db.exec("select * from users where id=?", [id])
                this.template("show.html")
            }
            if (req.method == "DEL")
            {
                this.ok = db.exec("delete from users where id=?", [id])
                this.redirect("/users")
            }
           
            if (req.method == "PUT")
            {
                this.ok = db.exec("update users  set (name=?, sex=?) where id=?",[req("name"), req("sex"),id]);
                this.redirect("/users/" + id)
            }
            
        })
    })
    
    
})


</script>
































你可能感兴趣的:(JavaScript,ITeye,Flash,asp,IIS)