日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Express框架实现原理

發布時間:2024/7/5 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Express框架实现原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Express 源碼的目錄結構


首先,會去package.json項目(包)描述文件中尋找main屬性的值,
main:入口文件。這個main的值就是入口文件所在的路徑。

這里并沒有配置main屬性的值,默認會去找index.js文件作為入口文件



二、快速體驗


const http = require('http') const url = require('url') const routes = [/*{ path: '', method: '', handler: () => {} }*/] 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 = [] } // const routes = [/*{ path: '', method: '', handler: () => {} }*/] 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.get = function(path, handler){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.get = function(path, handler){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.get = function(path, handler){Router.prototype[method] = function(path, handler){const layer = new Layer(path ,handler)layer.method = methodthis.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框架实现原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。