028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Node.js中怎么实现一个express框架

本篇文章给大家分享的是有关Node.js中怎么实现一个express框架,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联公司主营松滋网站建设的网络公司,主营网站建设方案,重庆APP软件开发,松滋h5小程序开发搭建,松滋网站营销推广欢迎松滋等地区企业咨询

express的基本用法

const express = require("express");  const app = express();  app.get("/test", (req, res, next) => {    console.log("*1");  //   res.end("2");    next();  });  app.get("/test", (req, res, next) => {    console.log("*2");    res.end("2");  });  app.listen(8888, (err) => {    !err && console.log("监听成功");  });
*1  *2

一起来实现一个简单的express框架

class express {  }  module.exports = express;
const { createServer } = require("http");
class express {    listen(...args) {      createServer(cb).listen(...args);    }  }

实现接收到请求触发

class express {    cb() {      return (req, res) => {        console.log(res, res, "开始行动");      };    }    listen(...args) {      createServer(this.cb()).listen(...args);    }  }
constructor() {      this.routers = {        get: [],        post: [],      };    }    get(path, handle) {      this.routers.get.push({        path,        handle,      });    }    post(path, handle) {      this.routers.post.push({        path,        handle,      });    }
cb() {    return (req, res) => {      const method = req.method.toLowerCase();      console.log(this.routers[method], ",method");      const url = req.url;      this.routers[method].forEach((item) => {        item.path === url && item.handle(req, res);      });    };  }  listen(...args) {    createServer(this.cb()).listen(...args);  }
[ { method: 'get', path: '/test', handle: [Function] } ] ,method

完成最重要的中间件功能

constructor() {     this.routers = {       get: [],       post: [],       all: [],     };   }
handleAddRouter(path, handle) {     let router = {};     if (typeof path === "string") {       router = {         path,         handle,       };     } else {       router = {         path: "/",         handle: path,      };     }     return router;   }   get(path, handle) {     const router = this.handleAddRouter(path, handle);     this.routers.get.push(router);   }   post(path, handle) {     const router = this.handleAddRouter(path, handle);     this.routers.post.push(router);   }   use(path, handle) {     const router = this.handleAddRouter(path, handle);     this.routers.all.push(router);   }

这里要注意,promise.then 源码实现和 express 的 next、以及 koa 的洋葱圈、redux 的中间件实现,有着一丁点相似,当你能真的领悟前后端框架源码时候,发现大都相似

实现next

search(method, url) {      const matchedList = [];      [...this.routers[method], ...this.routers.all].forEach((item) => {        item.path === url && matchedList.push(item.handle);      });      return matchedList;    }    cb() {      return (req, res) => {        const method = req.method.toLowerCase();        const url = req.url;        const matchedList = this.search(method, url);      };    }
handle(req, res, matchedList) {     const next = () => {       const midlleware = matchedList.shift();       if (midlleware) {         midlleware(req, res, next);       }     };     next();   }   cb() {     return (req, res) => {       const method = req.method.toLowerCase();       const url = req.url;       const matchedList = this.search(method, url);       this.handle(req, res, matchedList);     };   }

以上就是Node.js中怎么实现一个express框架,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


分享文章:Node.js中怎么实现一个express框架
文章来源:http://www.tsicrk.com/article/jghioi.html

其他资讯

让你的专属顾问为你服务

1.6205s