You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.2 KiB

  1. 'use strict'
  2. class Yeah {
  3. middleware () {
  4. return function (req, res, next) {
  5. res.end('Yeah?')
  6. next()
  7. }
  8. }
  9. }
  10. class Logger {
  11. middleware () {
  12. const express = require('express')
  13. const app = express()
  14. app.use((req, res, next) => {
  15. console.log('incoming', req.url)
  16. next()
  17. })
  18. return app
  19. }
  20. }
  21. class Header {
  22. middleware () {
  23. return function (req, res, next) {
  24. res.setHeader('x-pointless', 'yeah?')
  25. next()
  26. }
  27. }
  28. }
  29. class PieHeader {
  30. middleware () {
  31. const Koa = require('koa')
  32. const app = new Koa()
  33. app.use((ctx, next) => {
  34. ctx.set('x-pie', 'steak and kidney')
  35. next()
  36. })
  37. return app.callback()
  38. }
  39. }
  40. const http = require('http')
  41. const server = http.createServer()
  42. server.listen(8100)
  43. const yeah = new Yeah()
  44. const logger = new Logger()
  45. const header = new Header()
  46. const pie = new PieHeader()
  47. const stack = [
  48. logger.middleware(),
  49. header.middleware(),
  50. pie.middleware(),
  51. yeah.middleware()
  52. ]
  53. server.on('request', function (req, res) {
  54. let index = 0
  55. function processNext () {
  56. const mw = stack[index++]
  57. if (mw) mw(req, res, processNext)
  58. }
  59. processNext()
  60. })