Drake Izatt
Drake Izatt

Reputation: 31

React Router with Node JS

When using React-Router, you only serve up the original index.html with Node because the client handles the rest. I understand how to route between different pages once the user is at the home page. However, let's say the user types in the URL '/about'. Node will serve up the index.html, but how do you tell React-Router to then immediately take the user to the about page? Thank you.

Upvotes: 1

Views: 1973

Answers (1)

Skäggiga Mannen
Skäggiga Mannen

Reputation: 1043

If you have your BUILT create-react-app project sitting inside you node/public folder. you need to let express know that the index.html file will be handling the routes. see super simple server example below. this will use react routers setup properly.

const express = require('express');

const app = express();
const path = require('path');

app.use(express.static(path.join(__dirname, 'public')));
app.get('*', (req, res) => res.sendFile(path.join(__dirname, 'public/index.html')));

app.listen(3000);

Upvotes: 1

Related Questions