superagent不返回响应

问题描述 投票:0回答:1

我正在尝试以这种方式发送带有查询字符串的请求,但控制台中未记录任何内容。我想对返回的结果做点什么,但reserr都不是。

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);
});

这些代码有什么问题?

node.js superagent
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.