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傳過(guò)來(lái)的數(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è)試下,如果有任何疑問(wèn)和補(bǔ)充可以聯(lián)系小編,更多內(nèi)容可以查看以下相關(guān)知識(shí)點(diǎn)。
- Express的路由詳解
- 詳解NodeJS框架express的路徑映射(路由)功能及控制
- 詳解nuxt路由鑒權(quán)(express模板)
- nodejs開發(fā)——express路由與中間件
- 基于express中路由規(guī)則及獲取請(qǐng)求參數(shù)的方法
- nodeJS?express路由學(xué)習(xí)req.body與req.query方法實(shí)例詳解
- vue路由history模式頁(yè)面刷新404解決方法Koa?Express
- Node Express用法詳解【安裝、使用、路由、中間件、模板引擎等】
- nodejs?express路由匹配控制及Router模塊化使用詳解
- 淺探express路由和中間件的實(shí)現(xiàn)
- NodeJs?Express路由使用流程解析
- Express框架定制路由實(shí)例分析
相關(guān)文章
nodejs操作mysql實(shí)現(xiàn)增刪改查的實(shí)例
下面小編就為大家?guī)?lái)一篇nodejs操作mysql實(shí)現(xiàn)增刪改查的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
node.js中的fs.statSync方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.statSync方法使用說(shuō)明,本文介紹了fs.statSync的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
autojs的nodejs打包成品app經(jīng)驗(yàn)分享
這篇文章主要為大家介紹了autojs的nodejs打包成品app經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Node.js系列之發(fā)起get/post請(qǐng)求(2)
這篇文章主要為大家詳細(xì)介紹了Node.js系列之發(fā)起get/post請(qǐng)求,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Node.js檢測(cè)端口(port)是否被占用的簡(jiǎn)單示例
大家有沒(méi)有遇到過(guò)在開啟本地服務(wù)時(shí),有這么一種情況:當(dāng)前端口已經(jīng)被另一個(gè)項(xiàng)目使用了,導(dǎo)致服務(wù)開啟失敗。那么接下來(lái),我們通過(guò)簡(jiǎn)簡(jiǎn)單單的示例代碼來(lái)檢測(cè)端口是否已經(jīng)被占用。有需要的朋友們可以參考借鑒。2016-09-09
node的EventEmitter模塊基本用法簡(jiǎn)單實(shí)現(xiàn)示例
這篇文章主要為大家介紹了node的EventEmitter模塊基本用法簡(jiǎn)單實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
利用nginx + node在阿里云部署https的步驟詳解
這篇文章主要給大家介紹了關(guān)于利用nginx + node在阿里云部署https的步驟,文中通過(guò)圖文及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
完美解決node.js中使用https請(qǐng)求報(bào)CERT_UNTRUSTED的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決node.js中使用https請(qǐng)求報(bào)CERT_UNTRUSTED的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01

