【问题标题】:Can't modify protocol option of request object in sailsJS无法修改sailsJS中请求对象的协议选项
【发布时间】:2015-01-28 15:36:20
【问题描述】:

我正在使用库来检查 req.secure 选项以继续没有错误。但我的应用程序也部署到heroku,我使用自定义中间件检查“x-forwarded-proto”标头并将req.secure设置为true,req.protocol设置为“https”,但实际上这些选项在之后保持不变操作。在为 req.secure 和 req.protocol 赋值后,我立即进行了调试输出,我可以看到这些选项没有被修改。它肯定会通过该代码,因为我看到了“x-forwarded-proto”==“https”案例的调试信息。在 express js 中设置此类 var 有什么限制吗?

SailsJS v0.10.5

我在 config/http.js 中的自定义中间件代码:

if (req.headers['x-forwarded-proto'] === 'https'){
          req.secure = true;
          req.protocol = 'https';

          sails.log('Secure headers detected: secure - ', req.secure, ', protocol - ', req.protocol);
}

输出:

debug: Secure headers detected: secure - false , protocol - http

【问题讨论】:

    标签: javascript node.js heroku express sails.js


    【解决方案1】:

    其实我找到了这种行为的原因。在expressJS中定义的请求对象req具有protocolsecure这样的属性,这些属性是使用__defineGetter__函数定义的。此函数是V8 的特性,它设置的值在日志记录对象中不可见并且不可设置,仅在您使用__defineSetter__ 函数定义setter 的情况下.

    更多信息可以在这里找到:https://github.com/joyent/node/wiki/ecma-5-mozilla-features-implemented-in-v8#objectprototype

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多