表达req.ip返回对象
我有一个快速应用程序:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
语法是
expect(something).to.match
而不是expect(something).should.match
。请参阅文档。或者,如果您想使用should
,则不需要expect
,因为其语法是something.should.match
。因此,解决方法是更改您的代码,如下所示:
...或如下:
在样式指南中,您可以对 如何使用
expect
和 如何使用应该
。通过混合这两个东西,你得到了
expect(...)
,它返回包含to
之类的对象,并将其用作你的should
的源>,以便should.match
检查对expect(...)
返回的对象进行操作,而不是 IP 地址本身。The syntax is
expect(something).to.match
and notexpect(something).should.match
. See docs. Or, if you want to useshould
, you don't needexpect
, since the syntax for that issomething.should.match
.The fix is therefore to change your code either as follows:
...or as follows:
In the style guide, you get a good comparison between how to use
expect
and how to useshould
.By mixing those two things, you took
expect(...)
which returns object that contains things liketo
and used it as source of yourshould
, so thatshould.match
check operated on the object returned byexpect(...)
and not the IP address itself.