考虑以下代码:
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的源码
表示 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/