【问题标题】:Express: Cannot set property ip of #<IncomingMessage> which has only a getterExpress:无法设置只有 getter 的 #<IncomingMessage> 的属性 ip
【发布时间】:2021-01-09 22:38:26
【问题描述】:

我正在尝试创建一个中间件,将用户的 IP 设置为它为我们提供的 cloudflare 标头,用于应用程序的其余部分。这曾经适用于我的项目,但现在由于某种原因,它不起作用。

当我尝试导航到应用程序时,出现以下错误:

TypeError: Cannot set property ip of #<IncomingMessage> which has only a getter
    at /media/chen/storage/development/urlshortener/index.ts:67:11
    at Layer.handle [as handle_request] (/media/chen/storage/development/urlshortener/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/media/chen/storage/development/urlshortener/node_modules/express/lib/router/index.js:317:13)
    at /media/chen/storage/development/urlshortener/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/media/chen/storage/development/urlshortener/node_modules/express/lib/router/index.js:335:12)
    at next (/media/chen/storage/development/urlshortener/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/media/chen/storage/development/urlshortener/node_modules/body-parser/lib/types/json.js:110:7)
    at Layer.handle [as handle_request] (/media/chen/storage/development/urlshortener/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/media/chen/storage/development/urlshortener/node_modules/express/lib/router/index.js:317:13)
    at /media/chen/storage/development/urlshortener/node_modules/express/lib/router/index.js:284:7
app.use((req, res, next) => {
  req.ip = req.header('cf-connecting-ip') || req.ip; // Line 67, which the error is mentioning
  next()
})

如果我应该包含我的其余代码,请告诉我。这就是除了app.listen 之外的所有路由、中间件和快速设置都包含在其中的承诺

我使用的是 Express 4.17.1

【问题讨论】:

    标签: typescript express cloudflare


    【解决方案1】:

    您正在尝试设置请求属性为defined as getter

    在严格模式下运行时会引发错误。

    证明:

    (function() {
      'use strict';
      
      let obj = {};
    
      Object.defineProperty(obj, 'myProp', {
        configurable: true,
        enumerable: true,
        get: () => { return 'from getter' }
      });
    
      // with 'strict mode': Uncaught TypeError: Cannot set property myProp of #<Object> which has only a getter
      // without 'strict mode': executed silently and has no effect on the `myProp`.
      obj.myProp = 'explicitly set';
    
      // with 'strict mode': never reached
      // without 'strict mode': prints 'from getter'
      console.log(obj.myProp);
    }());
    

    您可以在请求对象上引入新属性,例如req.endUserIp 稍后在您的代码中使用它。

    由于您使用 TypeScript 标记了问题,因此您可能还想使用新属性 extend the Request interface

    【讨论】:

    • 另外,res.locals 可用于在中间件之间传递数据。
    • 当我尝试扩展请求接口时,当我尝试在路由或 app.use 中使用该属性时,Typescript 不断抛出错误,说Property 'realIp' does not exist on type 'Request&lt;CustomRequest, any, any, ParsedQs&gt;'.ts(2339),我必须继续使用// @ts-expect-error在我使用它的每一行上。扩展界面效果很好,打字稿出于某种原因不喜欢它。
    • @AbrarHossain 我一定会尝试利用它
    • @Flleeppyy 看起来 TS 没有看到您的接口声明。确保添加它的文件包含在 tsconfig 文件中。不过,这超出了这个问题的范围,您可能想在链接的 SO 帖子上查看其他答案和 cmets。干杯。
    • 是的,这就是问题所在!我已经解决了您链接的问题,谢谢!
    猜你喜欢
    • 2019-06-30
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2020-09-16
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多