R

Railway

βœ‹ο½œhelp

Join Server

PERN Application with React Router -- WebSocket connection to 'xxx' failed:

PPawtang5/24/2023
I read a previous thread where it was suggested to use serve when running the client in production mode, but this solution does not work with React Router in my understanding.

Could it be as simple as changing my client start script? This is my first time hosting an application.

"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
PPawtang5/24/2023
3bf7a56e-38fc-403a-92fa-0dcccfe80e97
Bbrody5/24/2023
react router is client side right?
PPawtang5/24/2023
Yessir
PPawtang5/24/2023
server scripts:
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "nodemon index.js &",
"run dev": "nodemon index.js"
},
Bbrody5/24/2023
does react router require the server to redirect all requests to index.html?
PPawtang5/24/2023
I'm all ears
PPawtang5/24/2023
What's bad here
Bbrody5/24/2023
^
PPawtang5/24/2023
I don't think so?
PPawtang5/24/2023
index.html is in the client side and only contains some metadata and the root for injecting the react build
Bbrody5/24/2023
is your react app a spa app?
PPawtang5/24/2023
const App = () => {
  return (
    <>
      <ToastManager />
      {/* <ToastManager toastStack={toastStack} /> */}
      <Routes>
        <Route path="/" element={<Landing />} />
        <Route path="about" element={<About />} />
        <Route path="contact" element={<Contact />} />
        <Route path="signup" element={<Signup />} />
        <Route path="login" element={<Login />} />
        <Route path="dashboard" element={<Dashboard />} />
        <Route path="suppliers" element={<ManagerSuppliers />} />
        <Route path="materials" element={<ManagerMaterials />} />
        <Route path="transactions" element={<ManagerTransactions />} />
      </Routes>
    </>
  );
};

ReactDOM.render(
  <BrowserRouter>
    <ToastProvider>
      <UserProvider>
        <App />
      </UserProvider>
    </ToastProvider>
  </BrowserRouter>,
  document.getElementById("root")
);
PPawtang5/24/2023
Bear with me I'm honestly quite stupid. I don't know if it counts as a SPA if it's technically rendering all of this content in the single html page "index.html" or if the inclusion of a router that builds URLs dynamically means its not a SPA
Bbrody5/24/2023
is there only one .html file named index.html in the build folder?
PPawtang5/24/2023
Yes
Bbrody5/24/2023
then its a single page app
PPawtang5/24/2023
Awesome πŸ™‚ thanks again for the support.
Bbrody5/24/2023
let me know if you need any clarification
PPawtang5/24/2023
I'll give it a shot now
PPawtang5/24/2023
Oh, serve should be installed in client folder yeah? or does it matter
PPawtang5/24/2023
vs global
Bbrody5/24/2023
it looks like you have modified your scripts quite a bit, please go back to the default start scripts before you start that guide
Bbrody5/24/2023
should be installed as a project dependency
Bbrody5/24/2023
and that is what npm i serve will do
PPawtang5/24/2023
Nice, I'm online, no web socket errors, client and server are happy. thanks.
Bbrody5/24/2023
sanity check, can you show me your current scripts?
PPawtang5/24/2023
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node index.js",
"run dev": "nodemon index.js"
},
PPawtang5/24/2023
"scripts": {
"dev": "react-scripts start",
"start": "serve build -s -n -L -p $PORT",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Bbrody5/24/2023
why did you send two
PPawtang5/24/2023
First was server. Irrelevant
Bbrody5/24/2023
ahhh
Bbrody5/24/2023
okay all looks good to me!