Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle client side react routing in loopback.js

I am using react with loopback. I wanted to integrate react code in loopback. if i do these 3 steps

1)middleware.json - put this
 "files": {
    "loopback#static": {
      "params":"$!../client"
    }
  },`
2)root.js
 router.get('/');
3)front end code
 "build": "react-scripts build && cp -r build/* ../client/",

That didopen my react site on localhost:3000 .Now issue is when i do this i cant access my loopback on :3000/explorer So my first question is in this scenario, how to access explorer.

But then i rolled it back , because i wanted to use loopback explorer again.

So, i deleted all these added code and explorer came back but when i added it again Now, i dont see my react code

I can still see explorer at http://localhost:3000/explorer/ if i go to http://localhost:3000/apphome i see 404 error

Right now, my middleware.json file for loopback is

    {
  "initial:before": {
    "loopback#favicon": {}
  },
  "initial": {
    "compression": {},
    "cors": {
      "params": {
        "origin": true,
        "credentials": true,
        "maxAge": 86400
      }
    },
    "helmet#xssFilter": {},
    "helmet#frameguard": {
      "params": [
        "deny"
      ]
    },
    "helmet#hsts": {
      "params": {
        "maxAge": 0,
        "includeSubdomains": true
      }
    },
    "helmet#hidePoweredBy": {},
    "helmet#ieNoOpen": {},
    "helmet#noSniff": {},
    "helmet#noCache": {
      "enabled": false
    }
  },
  "session": {},
  "auth": {},
  "parse": {
    "body-parser#json": {},
    "body-parser#urlencoded": {
      "params": {
        "extended": true
      }
    }
  },
  "routes": {
    "loopback#rest": {
      "paths": [
        "${restApiRoot}"
      ]
    }
  },
  "files": {
    "loopback#static": {
      "params":"$!../client"
    }
  },
  "final": {
    "loopback#urlNotFound": {},
    "./LoopbackUrlNotFoundCatch.js": {}
  },
  "final:after": {
    "strong-error-handler": {}
  }
}

root.js file

'use strict';
//router.get('/', server.loopback.status());
module.exports = function(server) {
  // Install a `/` route that returns server status
  var router = server.loopback.Router();

  router.get('/');
  server.use(router);
};

-edit

I made some changes. Now, i have react components showing, I can also see data when i use api routes. But, explorer is still missing.

middleware.json

"files": {
    "loopback#static": [
      {
        "name": "publicPath",
      "paths": ["/"],
      "params": "$!../client"
      },
      {
        "name": "reactRouter",
      "paths": ["*"],
      "params": "$!../client/index.html",
      "optional":true
      }
    ]
  },

I have also changed named of root.js to root_something.js . In documentation, its written, no need of root.js

like image 222
Ankur Sharma Avatar asked Dec 22 '17 20:12

Ankur Sharma


People also ask

How do you handle routing and navigation in react JS?

React Router overviewadd links for navigation. define the route of each page, meaning the URL path and the component that we want to load. define a router which will check if the requested URL is defined in the routes, and if it is, return the component.

Does ReactJS natively support client-side routing?

Client-side Routing in ReactReact renders the appropriate information on the DOM using its component structure. Client-side routing in React helps to maintain the seamless user experience that a typical single-page application promises. This is achieved through an external React library called React Router.

How does React work on client-side?

Client-Side Rendering By default, your React app will be client-side rendered. This means basically, all of the source code will live in JavaScript files referenced in a single HTML file that initializes your app.

How do you get past the route to React?

To go back to the previous page with React router: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function passing it -1 - navigate(-1) . Calling navigate with -1 is the same as hitting the back button.


1 Answers

First of all you Should be create react app in an other director like

Root ->
|-- client // emply
|-- clint_src // react app

and getting build react app and copy build files to client

now you should be remove server.loopback.status() in "server/boot/root.js" file

Example:

router.get('/', server.loopback.status());

To :

module.exports = function(server) {
  // Install a `/` route that returns server status
  var router = server.loopback.Router();
  router.get('/');
  server.use(router);
};

after that you need say to loopback middleware which file should be load in your path go to middlware /server/middleware.json and replace blow code to "files": {}

"files": {
    "loopback#static": [
      {
        "paths": ["/"],
        "params": "$!../client"
      },
      {
        "paths": ["*"],
        "params": "$!../client"
      }
    ]
  },

on "paths": ["*"], all route will be open react file exeption the "/api" and "explorer" so you should be handle page notfound inside react app

Hope This was help full Good Luck

like image 86
masoud soroush Avatar answered Oct 27 '22 01:10

masoud soroush