daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Jun 24th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. // var onHeaders = require('on-headers');
  3.  
  4. var app = express();
  5.  
  6.  
  7.  
  8. app.use(function(req, res, next){
  9.   console.log('middleware');
  10.   res.writeHead = createWriteHead(res.writeHead);
  11.   function createWriteHead(prevWriteHead){
  12.     var fired = false;
  13.     return function writeHead(statusCode) {
  14.       var args = setWriteHeadHeaders.apply(this, arguments);
  15.       if (!fired) {
  16.         fired = true
  17.         console.log('listener');
  18.         if (typeof args[0] === 'number' && this.statusCode !== args[0]) {
  19.           args[0] = this.statusCode
  20.           args.length = 1
  21.         }
  22.       }
  23.  
  24.       prevWriteHead.apply(this, args);
  25.     } // function writeHead(statusCode) {
  26.  
  27.  
  28.  
  29.  
  30.   } // createWriteHead
  31.  
  32.   function setWriteHeadHeaders(statusCode) {
  33.     var length = arguments.length
  34.     var headerIndex = length > 1 && typeof arguments[1] === 'string'
  35.       ? 2
  36.       : 1
  37.     var headers = length >= headerIndex + 1
  38.       ? arguments[headerIndex]
  39.       : undefined
  40.     this.statusCode = statusCode
  41.     if (Array.isArray(headers)) {
  42.       for (var i = 0, len = headers.length; i < len; ++i) {
  43.         this.setHeader(headers[i][0], headers[i][1])
  44.       }
  45.     } else if (headers) {
  46.       var keys = Object.keys(headers)
  47.       for (var i = 0; i < keys.length; i++) {
  48.         var k = keys[i]
  49.         if (k) this.setHeader(k, headers[k])
  50.       }
  51.     }
  52.     var args = new Array(Math.min(length, headerIndex))
  53.     for (var i = 0; i < args.length; i++) {
  54.       args[i] = arguments[i]
  55.     }
  56.     return args
  57.   } // function setWriteHeadHeaders(statusCode) {
  58.  
  59.  
  60.  
  61.   next();
  62. })// use
  63.  
  64.  
  65.  
  66.  
  67. // app.use(function(req, res, next){
  68. //   onHeaders(res, function(){
  69. //     // console.log(this);
  70. //     console.log('onHeaders');
  71. //     // res.setHeader('Content-Type', 'text/html')
  72. //   });
  73. //   next();
  74. // })
  75.  
  76. app.get('/', function(req, res){
  77.   res.set('Content-Type', 'text/html');
  78.   // res.set({
  79.   //   'Content-Type': 'text/plain',
  80.   //   'Content-Length': '123',
  81.   //   'ETag': '12345'
  82.   // })
  83.   // console.log(res.writeHead);
  84.   console.log('begin Hi!');
  85.   res.send('Hi!');
  86.   console.log('after Hi!');
  87. })
  88.  
  89. app.listen(3000);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top