2

I have a react application created using 'npm init react-app myapp'. I'm running the app by executing npm start. I need to run the app under a certain path like /myapp, similar to what we do with angular apps when we execute 'ng serve --base-href /myapp/.

I'm running the app on 'localhost:3000' I need to run it on 'localhost:3000/app'

How can I do this?

thanks

jishan siddique
  • 1,848
  • 2
  • 12
  • 23

3 Answers3

0

Your app should work for any path, so this is more a deployment configuration issue. You can run locally on /, then deploy on /app for production and just have the proper configuration of your code.

If path is not /, your need to define your base href element in the header of your index.html. Make sure it is before using any urls.

<base href="/app">

Best way to have full configuration is to save that value in environment variables like this or this.

sebastienbarbier
  • 6,542
  • 3
  • 29
  • 55
0

I am assuming you are using BrowserRouter for routing purposes in React.

Just add an attribute 'basename' which will accept the string of the base URL which you want to use.

In your case, it will be

<BrowserRouter basename="/app">
    ...
</BrowserRouter>
Nikhil Goyal
  • 1,945
  • 1
  • 9
  • 17
0

Any react router should work with basename prefix string.

Eg.

import { HashRouter } from 'react-router-dom'; 

<HashRouter basename="/app">
    <App />
</HashRouter>
J L P J
  • 442
  • 3
  • 8