Accept POST request in react-router

11,883

This is client side routing. You cannot post request here. You need to listen to post requests on your express js server.

In your serverjs:

app.post('/',function(req,res){
 res.send("hello")
})

That would do the trick.

Share:
11,883
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    My react-router is version 4.2.0

    I'm trying to accept a POST request to my React application. I may be blind, but I'm unable to find a way to specify a request method (GET, POST) on my routes.

    How can I set this up to accept POST requests?

    render((
        <BrowserRouter>
          <div>
            <Route exact path="/" history={history} render={() => (
                loggedIn() 
                    ? (<MyApp />)
                    : (<Redirect to="/login"/>)
            )}/>
            <Route exact path="/login" history={history}  render={() => (
                loggedIn() 
                    ? (<Redirect to="/"/>)
                    : (<Login />)
            )}/>
          </div>
        </BrowserRouter>
    ), document.getElementById('app'));
    

    Ideally, I would like to have a 3rd route that accepts a POST request to process the POST from a vendor.

    I found express-react-router but was unable to locate/install the "express-location" package.

    Any help would be appreciated.

  • Admin
    Admin about 6 years
    I knew that I could do it in my API, but I was hoping to do it directly in the react. Accepting because if there's no way to do it, then there's no way to do it. Thanks