Nodejs封裝類似express框架的路由實(shí)例詳解
代碼如下
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);
})
})
//登錄頁(yè)面
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://www.jygsgssxh.com/a1/JavaScript/9301.html
您可能感興趣的文章
- 01-10微信小程序封裝多張圖片上傳api代碼實(shí)例
- 01-10nodejs開發(fā)一個(gè)最簡(jiǎn)單的web服務(wù)器實(shí)例講解
- 01-10nodejs對(duì)mongodb數(shù)據(jù)庫(kù)的增加修刪該查實(shí)例代碼
- 01-10vue 對(duì)axios get pust put delete封裝的實(shí)例代碼
- 01-10原生js實(shí)現(xiàn)文件上傳、下載、封裝等實(shí)例方法
- 01-10nodeJs的安裝與npm全局環(huán)境變量的配置詳解
- 01-10Vue v-model組件封裝(類似彈窗組件)
- 01-10Nodejs + Websocket 指定發(fā)送及群聊的實(shí)現(xiàn)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02javascript點(diǎn)線,點(diǎn)線的代碼
- 04-02javascript潛力,javascript強(qiáng)大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報(bào),JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁(yè)面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


