欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

JavaScript

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > JavaScript >

Nodejs封裝類似express框架的路由實(shí)例詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:JavaScript|點(diǎn)擊: 次

代碼如下

var http=require('http');

var ejs=require('ejs');

var app=require('./model/express-route.js');

console.log(app);

http.createServer(app).listen(3000);

app.get('/',function(req,res){

  var msg='這是數(shù)據(jù)庫(kù)的數(shù)據(jù)'

  ejs.renderFile('views/index.ejs',{msg:msg},function(err,data){

    res.send(data);
  })
})


//登錄頁面
app.get('/login',function(req,res){

  console.log('login');

  ejs.renderFile('views/form.ejs',{},function(err,data){

    res.send(data);
  })

})

//執(zhí)行登錄
app.post('/dologin',function(req,res){

  console.log(req.body); /*獲取post傳過來的數(shù)據(jù)*/

  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('register');

  res.send('新聞數(shù)據(jù)');
})

express-route.js

var url=require('url');

//封裝方法改變r(jià)es 綁定res.send()
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;  /*全局變量*/

  //處理get和post請(qǐng)求
  this._get={};

  this._post={};



  var app=function(req,res){


    changeRes(res);

    //獲取路由
    var pathname=url.parse(req.url).pathname;
    if(!pathname.endsWith('/')){
      pathname=pathname+'/';
    }

    //獲取請(qǐng)求的方式 get post
    var method=req.method.toLowerCase();


    if(G['_'+method][pathname]){

      if(method=='post'){ /*執(zhí)行post請(qǐng)求*/

        var postStr='';
        req.on('data',function(chunk){

          postStr+=chunk;
        })
        req.on('end',function(err,chunk) {

          req.body=postStr; /*表示拿到post的值*/


          //G._post['dologin'](req,res)

          G['_'+method][pathname](req,res); /*執(zhí)行方法*/

        })



      }else{ /*執(zhí)行g(shù)et請(qǐng)求*/
        G['_'+method][pathname](req,res); /*執(zhí)行方法*/

      }

    }else{

      res.end('no router');
    }

  }

  app.get=function(string,callback){
    if(!string.endsWith('/')){
      string=string+'/';
    }
    if(!string.startsWith('/')){
      string='/'+string;

    }

    //  /login/
    G._get[string]=callback;

  }

  app.post=function(string,callback){
    if(!string.endsWith('/')){
      string=string+'/';
    }
    if(!string.startsWith('/')){
      string='/'+string;

    }
    //  /login/
    G._post[string]=callback;

    //G._post['dologin']=function(req,res){
    //
    //}
  }

  return app;

}

module.exports=Server();

以上代碼很簡(jiǎn)單,大家可以測(cè)試下,如果有任何疑問和補(bǔ)充可以聯(lián)系小編,更多內(nèi)容可以查看以下相關(guān)知識(shí)點(diǎn)。

上一篇:Vue學(xué)習(xí)之常用指令實(shí)例詳解

欄    目:JavaScript

下一篇:vue學(xué)習(xí)之Vue-Router用法實(shí)例分析

本文標(biāo)題:Nodejs封裝類似express框架的路由實(shí)例詳解

本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9301.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有