You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express模拟Microsoft Graph API返回404,请求排查原因

问题排查:Express Mock后端模拟Graph API时测试返回404

问题描述

我正在编写集成测试,用Express搭建Mock后端模拟Microsoft Graph API移除Entra组成员的请求。实际代码逻辑没问题,但测试一直返回404错误,找不到问题所在,请求帮忙排查。

相关代码

import axios from "axios";
import express from "express";
import { Server } from "http";

describe.only("fourohfour", () => {
  const app = express();
  app.use(express.json());
  // Entra remove member
  app.delete("graph/groups/:group/members/:userId/$ref", (req, res) => {
    console.log(`ENDPOINT remove ${req.params.group} ${req.params.userId}`);
    res.sendStatus(200);
  });

  let server: Server;

  beforeAll(done => {
    server = app.listen(3001, done);
  });

  afterAll(async () => {
    server.close();
  });

  it("should not four oh four", async () => {
    const res = await axios.delete(
      "http://localhost:3001/graph/groups/123/members/456/$ref",
    );

    expect(res.status).toBe(200);
  });
});

问题原因及解决方法

  • 路由路径缺少开头的斜杠:Express的路由路径如果不以/开头,会被视为相对路径,无法匹配请求的绝对路径。只需把路由路径改为以/开头即可。

修改后的路由代码:

app.delete("/graph/groups/:group/members/:userId/$ref", (req, res) => {
  console.log(`ENDPOINT remove ${req.params.group} ${req.params.userId}`);
  res.sendStatus(200);
});

修改后,测试请求的路径就能正确匹配到路由,不会再返回404错误。

内容的提问来源于stack exchange,提问作者martin8768

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:07:35