express-route.js

var url = require('url');
function changeRes(res){
    res.send= function(data){
        res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
        res.end(data);
    }
}

var Server = function(){
    var G = this;
    this._get = {};
    this._post = {};
    var app = function(req,res){
        changeRes(res);
        var pathname = url.parse(req.url).pathname;
        if(!pathname.endsWith('/')){
            pathname = pathname + '/';
        }
        var method = req.method.toLowerCase();
        // if(G['_'+ method][pathname]){
        if(G['_'+ method][pathname]){
            if(method == 'post'){
                var postStr = '';
                req.on('data',function(chunk){
                    postStr += chunk;
                })
                req.on('end',function(err,chunk){
                    req.body = postStr;
                    G['_'+method][pathname](req,res);
                })
            }else{
                G['_'+method][pathname](req,res);
            }
        }else{
            res.end('no router');
        }
    }
    app.get = function(string,callback){
        if(!string.endsWith('/')){
            string = string+'/'
        }
        if(!string.startsWith('/')){
            string = '/'+ string;
        }
        G._get[string] = callback;
    }
    app.post = function(string,callback){
        if(!string.endsWith('/')){
            string = string+'/'
        }
        if(!string.startsWith('/')){
            string = '/'+ string;
        }
        G._post[string] = callback;
    }
    return app;
}

module.exports = Server();

index2.js

var http = require('http');
var ejs = require('ejs');
var app = require('./model/express-route');
console.log(app);
http.createServer(app).listen(3000);
app.get('/',function(req,res){
    var msg = '这是数据库里的数据';
    ejs.renderFile('views/index.ejs',{msg:msg},function(err,data){
        res.send(data);
    })
})
app.get('/login',function(req,res){
    ejs.renderFile('views/form.ejs',{},function(err,data){
        res.send(data);
    })
})
app.post('/dologin',function(req,res){
    console.log(req.body);
    res.send("<script>alert('登录成功');history.back();</script>");
})
app.get('/register',function(req,res){
    console.log('register')
    res.send('register');
})
app.get('/news',function(req,res){
    console.log('news')
    res.send('news');
})

解析

http.createServer(app).listen(3000);
相当于

http.createServer(function(req,res){
    changeRes(res);
    var pathname = url.parse(req.url).pathname;
    if(!pathname.endsWith('/')){
        pathname = pathname + '/';
    }
    var method = req.method.toLowerCase();
    // if(G['_'+ method][pathname]){
    if(G['_'+ method][pathname]){
        if(method == 'post'){
            var postStr = '';
            req.on('data',function(chunk){
                postStr += chunk;
            })
            req.on('end',function(err,chunk){
                req.body = postStr;
                G['_'+method][pathname](req,res);
            })
        }else{
            G['_'+method][pathname](req,res);
        }
    }else{
        res.end('no router');
    }
}).listen(3000);

渣渣辉
1.3k 声望147 粉丝