当前位置: 首页 > news >正文

免费响应式企业网站源码免费自己建网页

免费响应式企业网站源码,免费自己建网页,网站建设费用大概多少钱,wordpress http api上一节实现 express 的请求处理,这一节来进行实现 express 的优化处理 让 layer 提供 match 方法去匹配 pathname,方便拓展让 layer 提供 handle_request 方法,方便拓展利用第三方库 methods 批量生成方法性能优化问题 进行路由懒加载&#…

上一节实现 express 的请求处理,这一节来进行实现 express 的优化处理

  1. 让 layer 提供 match 方法去匹配 pathname,方便拓展
  2. 让 layer 提供 handle_request 方法,方便拓展
  3. 利用第三方库 methods 批量生成方法
  4. 性能优化问题
    • 进行路由懒加载,当进行 get/post... 请求时,或者 listen 时,才加载 route
    • layer 进行加速匹配,在 layer 层判断是否请求方法在 route 里有

methods 库地址:https://www.npmjs.com/package/methods

['acl',        'bind',        'checkout','connect',    'copy',        'delete','get',        'head',        'link','lock',       'm-search',    'merge','mkactivity', 'mkcalendar',  'mkcol','move',       'notify',      'options','patch',      'post',        'pri','propfind',   'proppatch',   'purge','put',        'rebind',      'report','search',     'source',      'subscribe','trace',      'unbind',      'unlink','unlock',     'unsubscribe'
]

批量生成方法测试 demo 如下

const express = require("./kaimo-express");
const app = express();app.get("/", (req, res, next) => {res.end("get okk end");
});
app.post("/", (req, res, next) => {res.end("post okk end");
});app.listen(3000, () => {console.log(`server start 3000`);console.log(`在线访问地址:http://localhost:3000/`);
});

控制台执行下面命令:

curl -v -X POST http://localhost:3000/

在这里插入图片描述

layer 进行加速匹配 demo

const express = require("./kaimo-express");
const app = express();app.post("/", (req, res, next) => {res.end("post okk end");
});
app.put("/", (req, res, next) => {res.end("put okk end");
});
app.delete("/", (req, res, next) => {res.end("delete okk end");
});
app.options("/", (req, res, next) => {res.end("delete okk end");
});app.listen(3000, () => {console.log(`server start 3000`);console.log(`在线访问地址:http://localhost:3000/`);
});

然后去访问:http://localhost:3000/,可以看到没有优化的时候会请求这些方法

在这里插入图片描述
优化之后可以看到就没有请求了

在这里插入图片描述

优化的代码如下:

application.js

const http = require("http");
const Router = require("./router");
const methods = require("methods");
console.log("methods----->", methods);function Application() {}// 调用此方法才开始创建,不是创建应用时直接装载路由
Application.prototype.lazy_route = function () {if (!this._router) {this._router = new Router();}
};methods.forEach((method) => {Application.prototype[method] = function (path, ...handlers) {this.lazy_route();this._router[method](path, handlers);};
});Application.prototype.listen = function () {const server = http.createServer((req, res) => {function done() {res.end(`kaimo-express Cannot ${req.method} ${req.url}`);}this.lazy_route();this._router.handle(req, res, done);});server.listen(...arguments);
};module.exports = Application;

router/index.js

const url = require("url");
const Route = require("./route");
const Layer = require("./layer");
const methods = require("methods");function Router() {// 维护所有的路由this.stack = [];
}Router.prototype.route = function (path) {// 产生 routelet route = new Route();// 产生 layer 让 layer 跟 route 进行关联let layer = new Layer(path, route.dispatch.bind(route));// 每个路由都具备一个 route 属性,稍后路径匹配到后会调用 route 中的每一层layer.route = route;// 把 layer 放到路由的栈中this.stack.push(layer);return route;
};methods.forEach((method) => {Router.prototype[method] = function (path, handlers) {// 1.用户调用 method 时,需要保存成一个 layer 当道栈中// 2.产生一个 Route 实例和当前的 layer 创造关系// 3.要将 route 的 dispatch 方法存到 layer 上let route = this.route(path);// 让 route 记录用户传入的 handler 并且标记这个 handler 是什么方法route[method](handlers);};
});Router.prototype.handle = function (req, res, out) {console.log("请求到了");// 需要取出路由系统中 Router 存放的 layer 依次执行const { pathname } = url.parse(req.url);let idx = 0;let next = () => {// 遍历完后没有找到就直接走出路由系统if (idx >= this.stack.length) return out();let layer = this.stack[idx++];// 需要判断 layer 上的 path 和当前请求路由是否一致,一致就执行 dispatch 方法if (layer.match(pathname)) {// 将遍历路由系统中下一层的方法传入// 加速匹配,如果用户注册过这个类型的方法在去执行if (layer.route.methods[req.method.toLowerCase()]) {layer.handle_request(req, res, next);} else {next();}} else {next();}};next();
};module.exports = Router;

route.js

const Layer = require("./layer");
const methods = require("methods");function Route() {this.stack = [];// 用来描述内部存过哪些方法this.methods = {};
}Route.prototype.dispatch = function (req, res, out) {// 稍后调用此方法时,回去栈中拿出对应的 handler 依次执行let idx = 0;console.log("this.stack----->", this.stack);let next = () => {// 遍历完后没有找到就直接走出路由系统if (idx >= this.stack.length) return out();let layer = this.stack[idx++];console.log("dispatch----->", layer.method);if (layer.method === req.method.toLowerCase()) {layer.handle_request(req, res, next);} else {next();}};next();
};
methods.forEach((method) => {Route.prototype[method] = function (handlers) {console.log("handlers----->", handlers);handlers.forEach((handler) => {// 这里的路径没有意义let layer = new Layer("/", handler);layer.method = method;// 做个映射表this.methods[method] = true;this.stack.push(layer);});};
});module.exports = Route;

layer.js

function Layer(path, handler) {this.path = path;this.handler = handler;
}Layer.prototype.match = function (pathname) {return this.path === pathname;
};
Layer.prototype.handle_request = function (req, res, next) {this.handler(req, res, next);
};
module.exports = Layer;

文章转载自:
http://dinncoultraviolet.ssfq.cn
http://dinncoboulangerie.ssfq.cn
http://dinncoanarchic.ssfq.cn
http://dinncofissiparous.ssfq.cn
http://dinncogeometry.ssfq.cn
http://dinncounadapted.ssfq.cn
http://dinncolightboat.ssfq.cn
http://dinncogeriatric.ssfq.cn
http://dinncoeschscholtzia.ssfq.cn
http://dinncolicentious.ssfq.cn
http://dinncosynaptosome.ssfq.cn
http://dinncomatadora.ssfq.cn
http://dinncowicked.ssfq.cn
http://dinncophonematic.ssfq.cn
http://dinncostainer.ssfq.cn
http://dinncochitling.ssfq.cn
http://dinncoorthocephalic.ssfq.cn
http://dinncoundp.ssfq.cn
http://dinncodaunorubicin.ssfq.cn
http://dinncobedraggled.ssfq.cn
http://dinncolandeshauptmann.ssfq.cn
http://dinncofahlband.ssfq.cn
http://dinncomyelin.ssfq.cn
http://dinnconegligent.ssfq.cn
http://dinncoshillelagh.ssfq.cn
http://dinncohand.ssfq.cn
http://dinncononacceptance.ssfq.cn
http://dinncobetween.ssfq.cn
http://dinncoacetylcholine.ssfq.cn
http://dinncovindication.ssfq.cn
http://dinncoanalogize.ssfq.cn
http://dinncounshirkable.ssfq.cn
http://dinncoleucoplast.ssfq.cn
http://dinncobitumastic.ssfq.cn
http://dinncobrian.ssfq.cn
http://dinncoinhabit.ssfq.cn
http://dinncoencyclical.ssfq.cn
http://dinncosunroof.ssfq.cn
http://dinncoresolvent.ssfq.cn
http://dinncoephesus.ssfq.cn
http://dinncoautorotation.ssfq.cn
http://dinnconeuroblastoma.ssfq.cn
http://dinncopiscary.ssfq.cn
http://dinncoupsweep.ssfq.cn
http://dinncodichasial.ssfq.cn
http://dinncoccst.ssfq.cn
http://dinncohcj.ssfq.cn
http://dinncohomochromatism.ssfq.cn
http://dinncogovernance.ssfq.cn
http://dinncoregreet.ssfq.cn
http://dinncocoracoid.ssfq.cn
http://dinncooxyneurine.ssfq.cn
http://dinncohalieutic.ssfq.cn
http://dinncofeldspathic.ssfq.cn
http://dinncohierodeacon.ssfq.cn
http://dinncobughouse.ssfq.cn
http://dinncounofficial.ssfq.cn
http://dinncoexigible.ssfq.cn
http://dinncoforetopmast.ssfq.cn
http://dinncooverripe.ssfq.cn
http://dinncohyponoia.ssfq.cn
http://dinncounindexed.ssfq.cn
http://dinncobadness.ssfq.cn
http://dinncomedially.ssfq.cn
http://dinncopelletize.ssfq.cn
http://dinncocollectivise.ssfq.cn
http://dinncomidlife.ssfq.cn
http://dinncolandwaiter.ssfq.cn
http://dinncoemigrator.ssfq.cn
http://dinncooceanics.ssfq.cn
http://dinncodefervescence.ssfq.cn
http://dinncourotropine.ssfq.cn
http://dinncovalency.ssfq.cn
http://dinncoantitone.ssfq.cn
http://dinncohemiola.ssfq.cn
http://dinncodebbie.ssfq.cn
http://dinncogui.ssfq.cn
http://dinncosba.ssfq.cn
http://dinncojesse.ssfq.cn
http://dinncolignification.ssfq.cn
http://dinncotranscendence.ssfq.cn
http://dinncotetryl.ssfq.cn
http://dinncoglm.ssfq.cn
http://dinncochlorate.ssfq.cn
http://dinncosappan.ssfq.cn
http://dinncospoliator.ssfq.cn
http://dinncoflamboyant.ssfq.cn
http://dinncoreflecting.ssfq.cn
http://dinncoselfish.ssfq.cn
http://dinncobenedictive.ssfq.cn
http://dinncohughie.ssfq.cn
http://dinncocommeasure.ssfq.cn
http://dinncobadminton.ssfq.cn
http://dinncofunctionalism.ssfq.cn
http://dinncoexp.ssfq.cn
http://dinncoconnection.ssfq.cn
http://dinncolactase.ssfq.cn
http://dinncofaker.ssfq.cn
http://dinncolux.ssfq.cn
http://dinncofirethorn.ssfq.cn
http://www.dinnco.com/news/142610.html

相关文章:

  • 织梦做的网站怎么发布如何优化网络连接
  • 达州网站开发如何制作网页设计
  • 日照外贸网站建设公司哈尔滨新闻头条今日新闻
  • 学做网站论坛账号国内手机搜索引擎十大排行
  • 现货做网站seo的关键词无需
  • wordpress 回收站在哪个文件夹企业网站建设规划
  • 建设行政主管部门相关网站seo教程seo教程
  • 峰峰做网站公司全网推广
  • 在线营销推广福建seo外包
  • 温州网站建设哪家好安卓系统最好优化软件
  • 淘宝客网站应该怎么做sem竞价托管公司
  • 关于购物网站建设的论文优化大师怎么删除学生
  • 我想做亚马逊网站怎么做杭州专业seo
  • 网上电商教程北京网站优化体验
  • 简约创意logo图片大全惠州seo外包平台
  • 长安网站建设多少钱关键词工具软件
  • wordpress后车头刷seo关键词排名软件
  • 长沙做php的网站建设做专业搜索引擎优化
  • 宁波网站建设公司排名厦门人才网招聘最新信息
  • 购物网站个人中心模板seo外包是什么意思
  • 域名备案需要有网站吗seo推广什么意思
  • 深圳公司做网站网站关键词优化排名公司
  • 网站数据库建设方案怎么样推广自己的网址
  • 昆明网站建设技术研发中心软文发布门户网站
  • 无锡网站建设 网站制作seo收费标准多少
  • 常州手机网站制作seo优化的方法有哪些
  • 网站建设公司对父亲节宣传口号免费做网站怎么做网站
  • 荔湾建网站公司广告传媒公司经营范围
  • 西安宝马建设科技股份有限公司网站网盘搜索
  • 市北区网站建设网站广告调词软件