<Route>包含url参数时,React应用程序代理不起作用

时间:2019-11-06 20:52:41

标签: reactjs react-router

我有一个create react应用程序,通过代理与快递服务器对话 当我从大多数组件中发布时,代理可以工作,但是如果该组件是通过包含url参数的路由呈现的,则请求url会更改为不存在的内容,并导致404

我的Express服务器终结点(侦听端口5000)

    Sub CopyMeetingtoAppointment(oRequest As MeetingItem)

    If oRequest.MessageClass <> "IPM.Schedule.Meeting.Canceled" Then
    Exit Sub
    End If

    Dim oAppt As AppointmentItem
    Dim cAppt As AppointmentItem

    Set cAppt = oRequest.GetAssociatedAppointment(True)
    Set oAppt = Application.CreateItem(olAppointmentItem)

        oAppt.Subject = "(Rule) Canceled: " & cAppt.Subject
        oAppt.Start = cAppt.Start
        oAppt.Duration = cAppt.Duration
        oAppt.Location = cAppt.Location
        oAppt.Display
        oAppt.Save

       Set oAppt = Nothing
       Set cAppt = Nothing
    End Sub

可以成功卷曲

这是从具有方法的React组件形式中调用的

registrationRouter.post('/api/create-account' function (req, res) {

        return res.status(200);

});

路线包含;

    handleSubmit = (e) => {
        e.preventDefault();
        const { hash } = this.props.match.params;
     axios.post( api/create-account, {hash:hash});
    };

在package.json中定义的代理为

<Switch>
        <Route exact path='/' component={Home}/>
     {/*<Route path='/create-account/:hash' component={CreateAccountForm}/>*/} 
request is sent to create-account/api/create-account resulting in 404
        <Route path='/create-account' component={CreateAccountForm}/> 
request is sent to /api/create-account )resulting in 200
        <Route path='/' component={Home}/>
</Switch> 

在添加代理之前,这一切工作正常(当然,很难将发布网址编码为localhost:5000 / api / create-account)

自从我引入代理以来,这才开始发生。

我在做什么错?谢谢

1 个答案:

答案 0 :(得分:0)

我自己想通了。

答案在这里

How to fix wrong proxy redirection from sub path from React.js using React Router?

我需要在我的帖子请求中添加一个斜杠

 handleSubmit = (e) => {
        e.preventDefault();
        const { hash } = this.props.match.params;
     axios.post( api/create-account, {hash:hash});
    };