Encountered an unusual routing behavior in Express (node.js).
Attempting to test an API.
The server mounts:
app.use("/", productRouter);
app.use("/", testRouter);
productRouter.post("/products", ...);
testRouter.post("/test", ...);
If I POST
to:
http://localhost:MYPORT/test
http://localhost:MYPORT/products
Everything functions correctly.
However, when running this Mocha test:
const app = require("../src/index");
describe(" ====== Test for argumentMiddleware: a factory to check request arguments ======", () => {
describe("requiredArgument middleware", () => {
it("it should return error if requiredArgument is not in the body of request:", done => {
let requestBody = {
argument: "product"
};
chai
.request(app)
.post("/test")
.send(requestBody)
.end((err: Error, res: any) => {
res.should.have.status(400);
console.log(err.message);
done();
});
});
it.skip("it should pass if requiredArgument is in the body of request: ", done => {
let requestBody = {
requiredArgument: "product"
};
chai
.request(app)
.post("/test")
.send(requestBody)
.end((err: Error, res: any) => {
res.should.have.status(200);
done();
});
});
});
});
It seems to be POST
ing to /products
instead of /test
. A console.log
inside /products
is triggered. Why is it routing to that endpoint? Could it be due to its declaration order?