Skip to content

Instantly share code, notes, and snippets.

@LeeeeeeM
Created May 9, 2018 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeeeeeeM/0542a8bd0403e38cd00b8cf876d8db72 to your computer and use it in GitHub Desktop.
Save LeeeeeeM/0542a8bd0403e38cd00b8cf876d8db72 to your computer and use it in GitHub Desktop.
中间件实现express
function express() {
var middlewares = [];
var app = function(req, res) {
var i = 0;
function next() {
var task = middlewares[i++];
if (!task) {
return;
}
task(req, res, next);
}
setTimeout(next);
}
app.use = function(fn) {
middlewares.push(fn);
};
return app;
}
// var http = require('http');
// var app = express();
// http.createServer(app).listen(3000, function() {
// console.log('listening 3000');
// });
// function middleware1(req, res, next) {
// console.log('middleware1 before next()');
// next();
// console.log('middleware1 after next()');
// res.end('helloworld');
// }
// function middleware2(req, res, next) {
// console.log('middleware2 before next()');
// next();
// console.log('middleware2 after next()');
// }
// function middleware3(req, res, next) {
// console.log('middleware3 before next()');
// next();
// console.log('middleware3 after next()');
// }
// app.use(middleware1);
// app.use(middleware2);
// app.use(middleware3);
@LeeeeeeM
Copy link
Author

const express = function() {
  const middleware = []
  const app = function(req, res) {

  }

  app.use = function(fn) {
    middleware.push(fn)
  }

  app.start = function(req, res, context = null) {
    let i = 0
    function next() {
      const task = middleware[i++]
      if (!task || typeof task !== 'function') {
        return
      }
      task.call(context, req, res, next)
    }

    setTimeout(next.bind(context, req, res))
  }

  return app
}




const app = express()

app.use(middleware1)

app.use(middleware2)

app.use(middleware3)


function middleware1(req, res, next) {
  req.count++
  console.log(req, res, `middleware1 start`)
  next()
  res.count++
  console.log(req, res, `middleware1 end`)
}

function middleware2(req, res, next) {
  req.count++
  console.log(req, res, `middleware2 start`)
  next()
  res.count++
  console.log(req, res, `middleware2 end`)
}

function middleware3(req, res, next) {
  req.count++
  console.log(req, res, `middleware3 start`)
  next()
  res.count++
  console.log(req, res, `middleware3 end`)
}

app.start({
  flag: 'req',
  count: 0
}, {
  flag: 'res',
  count: 0
})



@LeeeeeeM
Copy link
Author

function express() {

  const middleWares = []

  let i = 0

  const context = app.context = {
    req: {
      list: []
    },
    res: {
      list: []
    }
  }

  function app() {
    function next() {
      let task = originTask = middleWares[i]
      if (i === 0) {
        task = function(...args) {
          originTask.call(context, ...args)
          app.done.call(context, ...args)
        }
      }
      i++
      if (task) {
        task(context, next)
      }
    }
    setTimeout(next, 0)
  }

  app.done = function () {

  }

  app.use = function(fn) {
    middleWares.push(fn)
  }

  return app
}

const app = express()

app.use(function(context, next) {
  context.req.list.push(1)
  next()
  context.res.list.push(1)
})

app.use(function(context, next) {
  context.req.list.push(2)
  next()
  context.res.list.push(2)
})

app.use(function(context, next) {
  context.req.list.push(3)
  next()
  context.res.list.push(3)
})

app.use(function(context, next) {
  context.req.list.push(4)
  next()
  context.res.list.push(4)
})

app.done = function(context) {
  console.log(this)
}

app()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment