我正在尝试测试我的API,我有以下错误:
未捕获错误:listen EADDRINUSE:地址已在使用:::5000
这是我的代码:
test.ts
chai.use(chaiHttp)
describe('Persons', () => {
describe('GET /person/', () => {
it('check error path', (done) => {
chai
.request(startServer)
.get("/persons/")
.end((err, res) => {
res.should.have.status(422);
})
done();
});
});
// describe('GET /person/:firstName', () => {
it('It should person by first name', (done) => {
chai
.request(startServer)
.get("/persons/Ross")
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('string');
// res.header["content-type"].should.contains('application/json');
})
done();
});
// });
});
server.ts
import express, { Application } from "express";
import personRouter from "./route/person.route";
import groupRouter from "./route/group.route";
import bodyParser from "body-parser";
const app: Application = express();
const PORT: Number | string = process.env.NODE_ENV || 5000;
app.use(bodyParser.json());
app.use('/person', personRouter)
app.use('/group', groupRouter)
export function startServer() {
app.listen(PORT, () => {
console.log(`Server started on http://localhost:${PORT}`);
});
}
我做错了什么?
我从另一个地方运行服务器,然后我这样做:
describe('Persons', () => {
const host = "http://localhost:5000/person/";
describe('GET /person/', () => {
it('check error path', (done) => {
chai
.request(host)
.get("/persons/")
.end((err, res) => {
res.should.have.status(200);
})
done();
});
});
这样服务器就不会试图启动每个测试。