无法在sailsJS中修改请求对象的协议选项

时间:2015-01-28 15:36:20

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

我使用检查req.secure选项的库继续而没有错误。但我的应用程序也部署到了heroku,我使用自定义中间件检查" x-forwarded-proto" header并将req.secure设置为true,将req.protocol设置为" https",但实际上这些选项在此类操作后保持不变。在将值分配给req.secure和req.protocol之后,我立即进行了调试输出,我可以看到这些选项没有被修改。它完全通过该代码,因为我看到" x-forwarded-proto"的调试信息。 ==" https"案件。在快递js中设置此类变量是否有任何限制?

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

1 个答案:

答案 0 :(得分:0)

实际上我找到了这种行为的原因。在 expressJS 中定义的请求对象 req 具有protocolsecure等属性,并使用__defineGetter__定义这些属性功能。此功能是V8的功能,它设置在日志记录中对象中不可见的值,不可设置,仅在您将使用{{1}定义setter的情况下功能。

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