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傳過來(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è)試下,如果有任何疑問和補(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)文章
node實(shí)現(xiàn)登錄圖片驗(yàn)證碼的示例代碼
這篇文章主要介紹了node實(shí)現(xiàn)登錄圖片驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-04-04
NodeJS學(xué)習(xí)筆記之Connect中間件模塊(一)
這是一個(gè)目錄概要,里面記錄著NodeJS的基礎(chǔ)知識(shí)部分,今天這篇文章以及后續(xù)的幾篇,將是一個(gè)進(jìn)階系列,讓我們建立一個(gè)由淺入深的學(xué)習(xí)的過程,2015-01-01
Linux環(huán)境部署node服務(wù)并啟動(dòng)詳細(xì)步驟
最近用node.js開發(fā)了一個(gè)web項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Linux環(huán)境部署node服務(wù)并啟動(dòng)的詳細(xì)步驟,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
使用Node.js創(chuàng)建一個(gè)簡(jiǎn)單的HTTP服務(wù)器的示例代碼
Node.js 是一種強(qiáng)大的 JavaScript 運(yùn)行環(huán)境,允許開發(fā)者在服務(wù)器端運(yùn)行 JavaScript 代碼,它以異步事件驅(qū)動(dòng)的方式處理大量連接,適合構(gòu)建高效的網(wǎng)絡(luò)應(yīng)用程序,在這篇文章中,我們將一起學(xué)習(xí)如何使用 Node.js 創(chuàng)建一個(gè)簡(jiǎn)單的 HTTP 服務(wù)器,并通過示例代碼幫你快速上手2025-02-02
基于node.js express mvc輕量級(jí)框架實(shí)踐
下面小編就為大家?guī)?lái)一篇基于node.js express mvc輕量級(jí)框架實(shí)踐。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-09-09
json對(duì)象及數(shù)組鍵值的深度大小寫轉(zhuǎn)換問題詳解
這篇文章主要給大家介紹了關(guān)于json對(duì)象及數(shù)組鍵值的深度大小寫轉(zhuǎn)換問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Node.js中環(huán)境變量process.env的一些事詳解
這篇文章主要給大家介紹了關(guān)于Node.js中環(huán)境變量process.env的一些事,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用node.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10

