生活随笔
收集整理的這篇文章主要介紹了
Express框架实现原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Express 源碼的目錄結構
首先,會去package.json項目(包)描述文件中尋找main屬性的值,
main:入口文件。這個main的值就是入口文件所在的路徑。
這里并沒有配置main屬性的值,默認會去找index.js文件作為入口文件
二、快速體驗
const http
= require('http')
const url
= require('url')
const routes
= []
function createApplication() {return {get (path
, handler
) {routes
.push({path
,method
: 'get',handler
})},listen (...args) {const server
= http
.createServer((req, res) => {const { pathname
} = url
.parse(req
.url
)const method
= req
.method
.toLowerCase()const route
= routes
.find(route => route
.path
=== pathname
&& route
.method
=== method
)if (route
) {return route
.handler(req
, res
)}res
.end('404 Not Found.')})server
.listen(...args
)}}
}
module
.exports
= createApplication
三、抽取App模塊
const http
= require('http')
const url
= require('url')
function App() {this.routes
= []
}
App.prototype
.get = function (path, handler) {this.routes
.push({path
,method
: 'get',handler
})
}
App.prototype
.listen = function (...args) {const server
= http
.createServer((req, res) => {const {pathname
} = url
.parse(req
.url
)const method
= req
.method
.toLowerCase()const route
= this.routes
.find(route => route
.path
=== pathname
&& route
.method
=== method
)if (route
) {return route
.handler(req
, res
)}res
.end('404 Not Found.')})server
.listen(...args
)}
module
.exports
= App
四、提取路由模塊
const url
= require('url')let Router = function Router() {this.stack
= []
}Router.prototype
.get = function(path, handler){this.stack
.push({path
,method
: 'get',handler
})
}
Router.prototype
.handle = function(req, res){const {pathname
} = url
.parse(req
.url
)const method
= req
.method
.toLowerCase()const route
= this.stack
.find(route => route
.path
=== pathname
&& route
.method
=== method
)if (route
) {return route
.handler(req
, res
)}res
.end('404 Not Found.')
}
module
.exports
= Router
const http
= require('http')
const Router
= require('./router/index.js')
let App = function App() {this._router
= new Router()
}
App.prototype
.get = function (path, handler) {this._router
.get(path
, handler
)
}
App.prototype
.listen = function (...args) {const server
= http
.createServer((req, res) => {this._router
.handle(req
, res
)})server
.listen(...args
)
}
module
.exports
= App
五、處理不同的請求方法
六、更強大的路由路徑匹配模式(基本實現)
const url
= require('url')
const methods
= require('methods')
const pathRegexp
= require('path-to-regexp')
let Router = function Router() {this.stack
= []
}
methods
.forEach(method => {Router.prototype
[method
] = function(path, handler){this.stack
.push({path
,method
,handler
})}
})
Router.prototype
.handle = function(req, res){const {pathname
} = url
.parse(req
.url
)const method
= req
.method
.toLowerCase()const route
= this.stack
.find(route => {const keys
= []const regexp
= pathRegexp(route
.path
, keys
, {})const match
= regexp
.exec(pathname
)return match
&& route
.method
=== method
})if (route
) {return route
.handler(req
, res
)}res
.end('404 Not Found.')
}
module
.exports
= Router
七、處理動態路由路徑參數
const url
= require('url')
const methods
= require('methods')
const pathRegexp
= require('path-to-regexp')
let Router = function Router() {this.stack
= []
}
methods
.forEach(method => {Router.prototype
[method
] = function(path, handler){this.stack
.push({path
,method
,handler
})}
})
Router.prototype
.handle = function(req, res){const {pathname
} = url
.parse(req
.url
)const method
= req
.method
.toLowerCase()const route
= this.stack
.find(route => {const keys
= []const regexp
= pathRegexp(route
.path
, keys
, {})const match
= regexp
.exec(pathname
)console
.log('keys=>', keys
)console
.log('match=>', match
)if (match
) {req
.params
= req
.params
|| {}keys
.forEach((key, index) => {req
.params
[key
.name
] = match
[index
+ 1]})}return match
&& route
.method
=== method
})if (route
) {return route
.handler(req
, res
)}res
.end('404 Not Found.')
}
module
.exports
= Router
八、提取Layer處理模塊
const url
= require('url')
const methods
= require('methods')
const Layer
= require('./layer.js')
let Router = function Router() {this.stack
= []
}
methods
.forEach(method => {Router.prototype
[method
] = function(path, handler){const layer
= new Layer(path
,handler
)layer
.method
= method
this.stack
.push(layer
)}
})
Router.prototype
.handle = function(req, res){const {pathname
} = url
.parse(req
.url
)const method
= req
.method
.toLowerCase()const route
= this.stack
.find(layer => {const match
= layer
.match(pathname
)if(match
) {req
.params
= req
.params
|| {}Object
.assign(req
.params
, layer
.params
)}return match
&& layer
.method
=== method
})if (route
) {return route
.handler(req
, res
)}res
.end('404 Not Found.')
}
module
.exports
= Router
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的Express框架实现原理的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。