node笔记(八)-Nodejs 路由封装 模块化方式封装

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

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);
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看