【问题标题】:Why my Node.js ServerResponse **wrapped by Proxy** doesn't respond?为什么我的 Node.js ServerResponse **由 Proxy 包装**没有响应?
【发布时间】:2020-06-13 19:40:54
【问题描述】:

这一定是一个非常具体和奇怪的问题。

这很明显,

import http from 'http';

http.createServer(function(_req, res) {
  res.end('yeah!');
}).listen(3000);

但事实并非如此。服务器不响应请求。

import http from 'http';

http.createServer(function(_req, res) {
  const pres = new Proxy(res, {});
  pres.end('yeah!');
}).listen(3000);

出于某种原因,我需要包装 ServerResponse... 我正在调试但不知道。 这样的代理对象与原始对象有何不同? 符号?属性定义?如果有人知道这件事,请发帖。任何信息将不胜感激。

【问题讨论】:

    标签: javascript node.js ecmascript-next


    【解决方案1】:

    我想通了。函数上下文 (this) 应该是原始的 ServerResponse 本身。

    import http from 'http';
    
    http.createServer(function(_req, res) {
      const pres = new Proxy(res, {
        get(t, p, r) {
          const v = Reflect.get(t, p, r);
          if (typeof v === 'function') return v.bind(t);
          return v;
        },
      });
      pres.end('yeah!');
    }).listen(3000);
    

    大。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 2020-04-14
      • 2021-11-08
      相关资源
      最近更新 更多