我正在尝试以这种方式发送带有查询字符串的请求,但控制台中未记录任何内容。我想对返回的结果做些什么,但 res 和 err 都不是。
superagent
.get('http://localhost:4000/v1/process/web')
.query({user: 'nux'})
.then(res => console.log(res.body))
.catch(e => console.error(e));
这是路由器的外观
const router = express.Router();
router.get('/web', async (req, res) => {
res.send(req.query);
});
这些代码有什么问题?
答案 0 :(得分:0)
这是一个最小的工作示例:
app.js
:
const express = require("express");
const router = express.Router();
const app = express();
router.get("/web", (req, res) => {
res.send(req.query);
});
app.use("/v1/process", router);
module.exports = app;
app.test.js
:
const app = require("./app");
const superagent = require("superagent");
const { expect } = require("chai");
describe("57953567", () => {
let server;
const port = 4000;
before((done) => {
server = app.listen(port, () => {
console.info(`HTTP server is listening on http://localhost:${server.address().port}`);
done();
});
});
after((done) => {
server.close(done);
});
it("should pass", () => {
return superagent
.get("http://localhost:4000/v1/process/web")
.query({ user: "nux" })
.then((res) => {
console.log(res.body);
expect(res.body).to.be.eql({ user: "nux" });
expect(res.status).to.be.equal(200);
});
});
});
覆盖率100%的集成测试结果:
57953567
HTTP server is listening on http://localhost:4000
{ user: 'nux' }
✓ should pass
1 passing (39ms)
-------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
app.js | 100 | 100 | 100 | 100 | |
app.test.js | 100 | 100 | 100 | 100 | |
-------------|----------|----------|----------|----------|-------------------|
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/57953567