Nodejs 路由模塊封裝、封裝仿照 express 的路由(9)

1、 模塊化的方式封裝

`model/model.js`html

 

// 導入模塊
var ejs=require('ejs'); var fs=require('fs'); var app ={ // login
 login:function (req, res) { console.log('login'); ejs.renderFile('views/form.ejs',{},function (err,data) { res.end(data); }) }, // dologin
 dologin:function (req, res) { console.log('333dologin'); var postStr = ''; req.on('data',function (chunk) { postStr+= chunk; }); req.on('end',function (err,chunk) { console.log(postStr); fs.appendFile('login.txt',postStr+'\n',function (err) { if (err){ console.log(err); return false; } console.log('寫入成功') }); res.end("<script>alert('登陸成功');history.back();</script>") }); }, // register
 register:function (req, res) { console.log('register'); res.end('register') }, home:function (req, res) { console.log('home'); res.end('home') } }; // 暴露
module.exports=app; /* 調用方式 app.login(req,res) app['login'](req,res) */

 

` 01 services.js`node

//引入http模塊
var http=require('http'); var url =require('url'); // 導入本身封裝的路由
var model = require('./model/model'); //路由:指的就是針對不一樣請求的 URL,處理不一樣的業務邏輯。
http.createServer(function(req,res){ // http://localhost:8001/login
    res.writeHead(200,{"Content-Type":"text/html;charset='utf-8'"}); var pathname = url.parse(req.url).pathname.replace('/','') console.log(pathname); // 調用路由,處理邏輯 
    if (pathname!=='favicon.ico'){ try{ model[pathname](req,res); }catch (err) { model['home'](req,res) } } }).listen(8001);

 

2、 封裝仿照 express 的路由

1、nodejs 萬事萬物皆爲對象

  構建一個app對象,能夠再綁定屬性和方法express

var app=function(){ console.log('app'); } app.get=function(){ console.log('app.get'); } app.post=function(){ console.log('app.post'); } app.post() /*app.post*/

// app()   /*app*/

 

2 仿照 express,封裝路由app

```模塊化

代碼執行順序: 從上到下函數

```post

var G ={}; var app =function (req, res) { if (G['login']){ G['login'](req,res);/*執行註冊方法*/ } }; // 定義一個get 方法
app.get = function (string, callback) { G[string]=callback // G[string] = 函數
}; // 執行get方法 login 頁面的處理邏輯
app.get('login',function (req, res) { console.log('login'+req) }); 
// 程序開始執行 setTimeout(
function () { app('req','res') },1000);

 

再次升級 `03 router_express_2.js` ui

var http=require('http'); var url= require('url'); var G ={}; var app =function (req, res) { var pathname = url.parse(req.url).pathname.replace('/',''); console.log(pathname); if (G[pathname]){ G[pathname](req,res); } // if (G['login']){
    // G['login'](req,res);/*執行註冊方法*/
    // }
}; // 定義一個get 方法
app.get = function (string, callback) { G[string]=callback // G[string] = 函數
}; // 只須要請求,就會觸發app這個方法
http.createServer(app).listen(8001); // 註冊login 路由
app.get('login',function (req, res) { console.log('login'); res.end('login'); }); // 註冊 register 路由
app.get('register',function (req, res) { console.log('register'); res.end('register'); });

 

最後的封裝this

`model/express_router.js`url

var url = require('url'); // 封裝 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請求
    this._get={}; this._post={}; var app=function (req, res) { // res 封裝send方法
 changeRes(res); var pathname = url.parse(req.url).pathname.replace('/',''); var method = req.method.toLowerCase(); console.log(method,pathname); console.log(G['_'+method]); if (G['_'+method][pathname]){ if (method ==='post'){ /* 處理 post 請求 */
                var postStr =''; req.on('data',function (chunk) { postStr+=chunk }); req.on('end',function (err, chunk) { req.body = postStr; G['_'+method][pathname](req,res); /*執行方法*/
                    // res.end(pathname)
 }) }else{ /* 處理非 post 請求 */ G['_'+method][pathname](req,res) // res.end(pathname)
 } }else{ res.end('no router!') } }; // 註冊方式
    app.get=function (string, callback) { console.log( G._get[string],string); G._get[string]=callback }; app.post=function (string, callback) { G._post[string]=callback }; return app }; module.exports = Server();

 

執行程序 `router_express_module.js`

var http = require('http'); var ejs = require('ejs'); var app = require('./model/express_router.js'); console.log(app); http.createServer(app).listen(8001); //登陸頁面
app.get('login',function(req,res){ ejs.renderFile('views/form.ejs',{},function(err,data){ res.send(data); }) }); app.post('dologin',function (req, res) { res.send("<script>alert('successful !');history.back();</script>") }); app.get('news',function (req, res) { res.send("國際大新聞") });
相關文章
相關標籤/搜索