手写一个express系列

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框架
                
                分享地址:http://www.csdahua.cn/qtweb/news22/330822.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网