gpt4 book ai didi

javascript - 为什么 Express 属性 ip 没有出现在 Object.keys(request) 中?

转载 作者:可可西里 更新时间:2023-11-01 16:59:44 27 4
gpt4 key购买 nike

考虑以下代码:

const express = require('express');
const app = express();
app.get('/', function(request, response) {
console.log(Object.keys(request));
console.log(request.ip);
});
app.listen(1337);

在第一个日志中我们可以看到 ip 不是 request 的属性,但在第二个日志中打印了 ip。为什么会这样?

最佳答案

根据Express的源码

In this particular line

表示 ip 索引不在请求对象中,express 默认将该索引作为函数返回 trust proxy fn 的值并返回它通过传递给它一个 proxyaddr

Object.keys(Object) <- This only returns the indices that appear in the object.

那么 Express Object.keys(request) 的那些动态和自动生成的索引是如何找不到它的。

关于javascript - 为什么 Express 属性 ip 没有出现在 Object.keys(request) 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47876834/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com