Friday 6 November 2020

react router with flask back end

I am building a project with react as frontend and flask as backend. I want the application created by 'create-react-app' to use front end routing with 'react-router-dom' package. Relevant code in index.js:

<BrowserRouter>
  <Switch>
    <Route exact path="/" component={Home} />
    <Route path="/about" component={About} />
    <Route component={Notfound} />
  </Switch>
</BrowserRouter>

Then I want to serve it via flask, so in flask I have following settings and routing rules:

app = Flask(__name__, static_folder="../build/static", template_folder="../build")

@app.route('/', defaults={'path': ''})
def serve(path):
    render_template("index.html")

Where index.html is built with npm run build. When I click on navbar and redirect to /about, app returns 404 not found.

Also there's another error in the browser console, which shows: Manifest: Line: 1, column: 1, Unexpected token.

Any help is appreciated.



from react router with flask back end

No comments:

Post a Comment