node 노트 (8) - Nodejs 루트 패키지 모듈 화 방식 패키지

3624 단어 데이터 뱅 크
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("alert('    ');history.back();");
})
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(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);

좋은 웹페이지 즐겨찾기