Express: 无法设置只有 getter 的 #<IncomingMessage> 的 属性 ip
Express: Cannot set property ip of #<IncomingMessage> which has only a getter
我正在尝试创建一个中间件,将用户的 IP 设置为它为我们提供的 cloudflare header,用于应用程序的其余部分。这曾经适用于我的项目,但现在由于某种原因,它不起作用。
当我尝试导航到应用程序时,出现以下错误:
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
您正在尝试设置 属性 的请求 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 标记了问题,您可能还想 使用新的 属性。
我正在尝试创建一个中间件,将用户的 IP 设置为它为我们提供的 cloudflare header,用于应用程序的其余部分。这曾经适用于我的项目,但现在由于某种原因,它不起作用。
当我尝试导航到应用程序时,出现以下错误:
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
您正在尝试设置 属性 的请求 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 标记了问题,您可能还想