Deno 部署 - 请求时未定义 IP 地址

Lum*_*uma 5 mongodb node.js express deno express-rate-limit

我在 Deno 上使用expressmongo,并且我也尝试使用express-rate-limit,但是,我在速率限制上收到以下错误,因为request.ip未定义:

ValidationError: An undefined 'request.ip' was detected. This might indicate a misconfiguration or the connection being destroyed prematurely. See https://express-rate-limit.github.io/ERR_ERL_UNDEFINED_IP_ADDRESS/ for more information.
    at Object.ip (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:97:13)
    at Object.wrappedValidations.<computed> [as ip] (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:296:22)
    at Object.keyGenerator (file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:549:20)
    at file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:601:32
    at Object.runMicrotasks (ext:core/01_core.js:934:26)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:53:10)
    at runNextTicks (ext:deno_node/_next_tick.ts:71:3)
    at eventLoopTick (ext:core/01_core.js:189:21)
    at async file:///node_modules/.deno/express-rate-limit@7.1.5/node_modules/express-rate-limit/dist/index.mjs:583:5 {
  name: "ValidationError",
  code: "ERR_ERL_UNDEFINED_IP_ADDRESS",
  help: "https://express-rate-limit.github.io/ERR_ERL_UNDEFINED_IP_ADDRESS/"
}
Run Code Online (Sandbox Code Playgroud)

我在一个类似的问题上找到了这个答案,建议添加app.set('trust proxy', 1);我尝试过,但 ip 仍然未定义。

如何重现:

"use strict"

import express    from 'express';
import bodyParser from 'body-parser';
import mongoose   from 'mongoose';
import rateLimit  from 'express-rate-limit';

const app = express();

mongoose.connect("...");
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() { console.log('Connected to MongoDB'); });

app.use(bodyParser.json());
app.set('trust proxy', 1);

const limiter = rateLimit({  windowMs: 1 * 60 * 1000,  max: 3, });
app.post('/api/users', limiter, async (req, res) =>
{
    console.log('ip:', req.ip);
    console.log('conn:', req.connection)
    console.log('conn.remoteAddress:', req.connection.remoteAddress)
    console.log('client:', req.client);
    console.log('headers:', req.headers);
    console.log('rawHeaders:', req.rawHeaders);
    console.log('X-Forwarded-For:', req.headers['x-forwarded-for']);
    console.dir('req:', req);
    try {
        const variable = await db.collection("users").findOne({ [req.body.user]: { $exists: true } })
        if (!variable) return res.status(404).send('Not found');
        res.send(variable);
    } 
    catch (error) { res.status(500).send('Internal Server Error');  }
});

app.listen(5000, () => { console.log(`running`); });
Run Code Online (Sandbox Code Playgroud)

在本地测试时,我可以在所有这些变量上看到我的 ip,而在 Deno 上,这就是打印的内容:

ip: undefined
conn: FakeSocket {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    remoteAddress: undefined,
    remotePort: undefined,
    encrypted: undefined,
    writable: true,
    readable: true,
    [Symbol(kCapture)]: false
  }
conn.remoteAddress: undefined
client: undefined
headers:
{
  "accept-encoding": "gzip, deflate",
  "accept-language": "en-US,en,*",
  "content-length": "14",
  "content-type": "application/json",
  host: "....deno.dev",
  "user-agent": "Mozilla/5.0"
}
rawHeaders: undefined
client: undefined
X-Forwarded-For: undefined
req: <ref *1> IncomingMessageForServer {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: [],
      flowing: true,
      ended: true,
      endEmitted: true,
      reading: false,
      constructed: true,
      sync: false,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      destroyed: true,
      errored: null,
      closed: true,
      closeEmitted: false,
      defaultEncoding: "utf8",
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      dataEmitted: true,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: false
    },
    _read: [AsyncFunction: read],
    _destroy: [Function: destroy],
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    url: "/api/users",
    method: "POST",
    socket: FakeSocket {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      remoteAddress: undefined,
      remotePort: undefined,
      encrypted: undefined,
      writable: true,
      readable: true,
      [Symbol(kCapture)]: false
    },
    next: [Function: next],
    baseUrl: "",
    originalUrl: "/api/users",
    _parsedUrl: Url {
      protocol: null,
      slashes: null,
      auth: null,
      host: null,
      port: null,
      hostname: null,
      hash: null
Run Code Online (Sandbox Code Playgroud)

ip 未定义,并且缺少所有标头,我不确定这是否是一个错误,或者如果我缺少某些内容,我们将不胜感激。