如何处理package.json“主页”属性和 API 端点

How to deal with package.json "homepage" attribute and api endpoints

提问人:Pamwamba 提问时间:7/12/2023 更新时间:7/12/2023 访问量:22

问:

我正在尝试将一个angularjs项目迁移到React(从头开始),并且在调用第一个应该返回特定配置的端点时遇到了一个奇怪的问题。

出于某些原因,我不得不在我的package.json中添加“homepage”: “/front-react”。所以 angularJS 应用程序在根目录中运行,我们可以从 /front-react 访问 react 应用程序(还有更多我无法显示的后端 conf,但这就是想法)。这东西有效,没问题。

我正在使用“createApi”RTK Query进行查询,如下所示:

export const loginApi = createApi({
  reducerPath: 'loginApi',
  baseQuery: fetchBaseQuery({
    baseUrl: '/',
    headers: {
      pragma: 'no-cache',
      Accept: 'application/json',
    },
  }),
  endpoints: (builder) => ({
    getConfiguration: builder.query<any, void>({
      query: () => ({
        url: `/rest-api/public/configuration`,
      }),
    }),
  }),
});

现在在我的登录页面中,我只是这样做了:

const { data: configuration} = useGetConfigurationQuery();

这就是事情变得奇怪的地方。

首先,我得到了 200 状态,但响应是我的 index.html。

我尝试添加标头 Accept: application/json,现在我得到了 302 状态,然后是 404。 302 在端点之前使用“/front-react”触发另一个 api 调用,因此它看起来像“/front-react/rest-api/public/configuration”而不是“/rest-api/public/configuration”(这就是我得到 404 的原因)。

我尝试使用http-proxy-middleware添加setupProxy.js,但没有更好的了。

我不知道下一步该怎么做,我不知道我是否在反应中缺少配置。

感谢您的阅读

(我试过邮递员,端点工作正常)

reactjs 代理 http-status-code-302

评论


答: 暂无答案