表达req.ip返回对象

发布于 2025-01-09 01:02:44 字数 1269 浏览 2 评论 0原文

我有一个快速应用程序:

const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));
app.get('/api/whoami', (req, res) => {
    const ipaddress = req.ip;
    res.status(200).json({ ipaddress });
});
    
app.listen(process.env.PORT || 3000);    
module.exports = app;

和一个测试文件:

const chai = require('chai');

const chaiHttp = require('chai-http');
const chaiMatch = require('chai-match');
const { describe, it } = require('mocha');
const server = require('../../server');

const should = chai.should();
const { expect } = chai;
chai.use(chaiHttp);
chai.use(chaiMatch);

describe('/GET /api/whoami', () => {
  it('should return the IP address', (done) => {
    chai.request(server)
      .get('/api/whoami')
      .end((err, res) => {
        res.should.have.status(200);
        res.body.should.be.a('object');
        res.body.should.have.property('ipaddress');
        expect(res.body.ipaddress).should.match(/* very long regex */);
        done();
      });
  });
});

出于某种原因,我不断收到 Uncaught AssertionError: Expected Assertion{ __flags: { …(4) } } 来匹配 [我很长的正则表达式],我没有找到有同样错误的人。如何通过express获取我的真实ip?或者说正确的测试方法是什么?

I have an express app:

const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));
app.get('/api/whoami', (req, res) => {
    const ipaddress = req.ip;
    res.status(200).json({ ipaddress });
});
    
app.listen(process.env.PORT || 3000);    
module.exports = app;

and a test file:

const chai = require('chai');

const chaiHttp = require('chai-http');
const chaiMatch = require('chai-match');
const { describe, it } = require('mocha');
const server = require('../../server');

const should = chai.should();
const { expect } = chai;
chai.use(chaiHttp);
chai.use(chaiMatch);

describe('/GET /api/whoami', () => {
  it('should return the IP address', (done) => {
    chai.request(server)
      .get('/api/whoami')
      .end((err, res) => {
        res.should.have.status(200);
        res.body.should.be.a('object');
        res.body.should.have.property('ipaddress');
        expect(res.body.ipaddress).should.match(/* very long regex */);
        done();
      });
  });
});

for some reason, I keep geting Uncaught AssertionError: expected Assertion{ __flags: { …(4) } } to match [my very long regex], i didn't find anyone with the same error. How can I get my true ip with express? Or what is the right way to test it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2025-01-16 01:02:44

语法是 expect(something).to.match 而不是 expect(something).should.match。请参阅文档。或者,如果您想使用 should,则不需要 expect,因为其语法是 something.should.match

因此,解决方法是更改​​您的代码,如下所示:

expect(res.body.ipaddress).to.match(/* very long regex */);

...或如下:

res.body.ipaddress.should.match(/* very long regex */);

在样式指南中,您可以对 如何使用 expect如何使用应该

通过混合这两个东西,你得到了 expect(...) ,它返回包含 to 之类的对象,并将其用作你的 should 的源>,以便 should.match 检查对 expect(...) 返回的对象进行操作,而不是 IP 地址本身。

The syntax is expect(something).to.match and not expect(something).should.match. See docs. Or, if you want to use should, you don't need expect, since the syntax for that is something.should.match.

The fix is therefore to change your code either as follows:

expect(res.body.ipaddress).to.match(/* very long regex */);

...or as follows:

res.body.ipaddress.should.match(/* very long regex */);

In the style guide, you get a good comparison between how to use expect and how to use should.

By mixing those two things, you took expect(...) which returns object that contains things like to and used it as source of your should, so that should.match check operated on the object returned by expect(...) and not the IP address itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文